Home > Software engineering >  how to check value in previous week
how to check value in previous week

Time:10-28

I have table having values for week by week, I need to select another column to show value for previous week.

Select Week, Value, PreviousWeek'svalue from Data 
Week  Value     Previous WeeksValue 

Week1   11  
Week2   13           11
Week3   46           13
Week4   10           46
Week5   15           10

CodePudding user response:

select  *
       ,lag(Value) over(order by Week) as Previous_Weeks_Value
from    t
Week Value Previous_Weeks_Value
Week1 11 null
Week2 13 11
Week3 46 13
Week4 10 46
Week5 15 10

Fiddle

  •  Tags:  
  • sql
  • Related