Running difference month over month

╄→гoц情女王★ 提交于 2021-02-11 12:30:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!