Home > Software engineering >  How do I to get next row's column value in a stored procedure column in SQL
How do I to get next row's column value in a stored procedure column in SQL

Time:07-30

I need help please.

In my final select I want to display the next row's value in PriceValue column

Example of my stored procedure:

SELECT 
    w.ShopID AS shopID,
    w.ProductName AS ProductName,
    w.ProductID AS ProductID,
    w.PriceValue   -- (this logic should be: if w.Type = Outdoor then give the next row's value 
                   -- which would then be **100 000** and **90 000** see image)    
FROM 
    Warehouse w

enter image description here

Thank you!

CodePudding user response:

You can use LEAD() (for previous LAG()) function there. ie:

SELECT w.ShopID as shopID, w.ProductName as ProductName,
w.ProductID as ProductID,
w.ProductID as w.PriceValue, 
case when Type = 'Outdoor' then 
      Lead(PriceValue, 1, 0) over (Partition by ProductName order by ProductId) else PriceValue end as ModifiedPrice 
from myTable;

Note: Your data structure looks weird, that may be a typo.

  • Related