Cumulative Sum per item in DB2

ε祈祈猫儿з 提交于 2020-01-06 08:51:39

问题


I have DB2 table like below -

Date1       Item_code    Amt
2018-06-01  1            2
2018-06-02  1            3
2018-06-03  2            4
2018-06-03  2            5
2018-06-04  3            6
2018-06-05  3            7
2018-06-06  4            8

I need the cumulative sum item_code wise per day. The result should look like -

Date1       Item_code    Amt
2018-06-01  1            2
2018-06-02  1            5
2018-06-03  2            9
2018-06-04  3            6
2018-06-05  3            13
2018-06-06  4            8

I have tried a lot by myself and search also on SO but nothing is fulfilling my need. There are a lot of examples if I just need the cumulative sum day wise irrespective of item code.

Any help is greatly appreciated. Thanks in advance.


回答1:


I think you want aggregation with a cumulative sum:

select item_code, date1,
       sum(sum(amt)) over (partition by item_code order by date1) as running_amt
from t
group by item_code, date;


来源:https://stackoverflow.com/questions/51116723/cumulative-sum-per-item-in-db2

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