我使用Netezza和 我有这样的桌子。
cert_id date value
-------- ------------ ------
01 2018-01-01 2
01 2018-01-02 1
01 2018-01-03 3
02 2018-02-06 2
02 2018-02-07 1
02 2018-02-08 4
02 2018-02-09 6
我希望这张汇总表(随着时间的推移)像这样。
cert_id date value
-------- ------------ ------
01 2018-01-01 2
01 2018-01-02 3
01 2018-01-03 6
02 2018-02-06 2
02 2018-02-07 3
02 2018-02-08 7
02 2018-02-09 13
答案 0 :(得分:1)
一种方法是使用相关子查询来查找滚动总和:
SELECT
cert_id,
date,
"value",
(SELECT SUM(t2."value") FROM yourTable t2
WHERE t1.cert_id = t2.cert_id AND t2.date <= t1.date) rolling_sum
FROM yourTable t1
ORDER BY
cert_id,
date;
如果Netezza支持分析功能,那么这是一个更简单的查询:
SELECT
cert_id,
date,
"value",
SUM(value) OVER (PARTITION BY cert_id ORDER BY date) rolling_sum
FROM yourTable;