DB2 中每个项目的累积总和 [英] Cumulative Sum per item in DB2

查看:10
本文介绍了DB2 中每个项目的累积总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下 DB2 表 -

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

我需要每天累积的 item_code 总和.结果应该看起来像 -

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

我自己尝试了很多,也在 SO 上搜索,但没有什么能满足我的需求.如果我只需要每天累积总和而不考虑项目代码,那么有很多示例.

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.

推荐答案

我想你想要一个累积和的聚合:

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;

这篇关于DB2 中每个项目的累积总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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