Reputation: 59
I have a sample data, i want to get the Difference in month over month data 'Lag' column for only row B
Upvotes: 0
Views: 113
Reputation: 222572
If there always is just one row per month and id
, then just use lag()
. You can wrap this in a case
expression so it only applies to id
'B'
.
select
id,
date,
data,
case when id = 'B'
then data - lag(data) over(partition by id order by date)
end lag_diff
from mytable
Upvotes: 1