【发布时间】:2017-08-14 10:32:56
【问题描述】:
我正在尝试对 Redshift 中的数据集执行窗口函数,使用天数作为前行的间隔。 示例数据:
date ID score
3/1/2017 123 1
3/1/2017 555 1
3/2/2017 123 1
3/3/2017 555 3
3/5/2017 555 2
最近 3 个分数的平均分数的 SQL 窗口函数:
select
date,
id,
avg(score) over
(partition by id order by date rows
between preceding 3 and
current row) LAST_3_SCORES_AVG,
from DATASET
结果:
date ID LAST_3_SCORES_AVG
3/1/2017 123 1
3/1/2017 555 1
3/2/2017 123 1
3/3/2017 555 2
3/5/2017 555 2
问题是我想要最近 3 天(移动平均)的平均分数,不是最近三个测试。我已经查看了 Redshift 和 Postgre 文档,但似乎找不到任何方法。
期望的结果:
date ID 3_DAY_AVG
3/1/2017 123 1
3/1/2017 555 1
3/2/2017 123 1
3/3/2017 555 2
3/5/2017 555 2.5
任何方向都将不胜感激。
【问题讨论】:
-
编辑您的问题并显示您想要的结果。
标签: sql amazon-redshift window-functions