PostgreSQL累计总和最近3天

时间:2019-12-15 15:01:30

标签: sql postgresql aggregate-functions window-functions

我有此数据,我想像下面那样计算3天的滚动总和。

注意:必须考虑缺少date。因此,不仅是最后3行,而且是3个太阳日。

enter image description here enter image description here

我尝试了使用窗口函数,但是它不起作用:

SELECT
    date,
    SUM(clicks) OVER (ORDER BY DATE ASC ROWS BETWEEN DATE - INTERVAL '3 day' AND date)
FROM table

1 个答案:

答案 0 :(得分:1)

上限应为current rowrange

SELECT
    date,
    SUM(clicks) OVER (ORDER BY DATE ASC range BETWEEN INTERVAL '3 day' PRECEDING AND CURRENT ROW)
FROM table