AbbaB
AbbaB

Reputation: 23

SQL assign a value to a variable but taking that value from another row

I want the Superior_Val to be equal to 'Value' of the next year, like:

Year    Value    Superior_Val
0       0.1      0.3
1       0.3      0.7
2       0.7      1.6
3       1.6      

How can I make it work something like (This is erroneous but it's just to understand the goal I want to achieve):

Superior_Val=Value where Year=Year+1

Upvotes: 1

Views: 32

Answers (2)

P.Salmon
P.Salmon

Reputation: 17665

Lots of ways to do this, if year is reliable then a self join

select a.year,a.value, b.value
from  t a
left join t b on b.year = a.year + 1

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271131

You can use lead():

select t.*, lead(value) over (order by year) as next_value
from t;

Upvotes: 1

Related Questions