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

查看:121
本文介绍了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天全站免登陆