问题
I have a sample data, i want to get the Difference in month over month data 'Lag' column for only row B
回答1:
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
来源:https://stackoverflow.com/questions/62160736/running-difference-month-over-month