每月运行差异 [英] Running difference month over month

查看:54
本文介绍了每月运行差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例数据,我只想获取B行的月比数据滞后"列

I have a sample data, i want to get the Difference in month over month data 'Lag' column for only row B

推荐答案

如果每个月总是只有一行和 id ,则只需使用 lag().您可以将其包装在 case 表达式中,这样它仅适用于 id 'B'.

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

这篇关于每月运行差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆