带天的红移SQL窗口函数框架_子句

带天的红移SQL窗口函数框架_子句,sql,amazon-redshift,window-functions,Sql,Amazon Redshift,Window Functions,我正在尝试对红移中的数据集执行一个窗口函数,使用前几行的天间隔。 示例数据: 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

我正在尝试对红移中的数据集执行一个窗口函数,使用前几行的天间隔。 示例数据:

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天移动平均线的平均分数,而不是最后三次测试的平均分数。我查阅了红移和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
任何指示都将不胜感激

您可以使用lag并显式计算平均值

select t.*,
       (score +
        (case when lag(date, 1) over (partition by id order by date) >=
                   date - interval '2 day'
              then lag(score, 1) over (partition by id order by date)
              else 0
         end) +
        (case when lag(date, 2) over (partition by id order by date) >=
                   date - interval '2 day'
              then lag(score, 2) over (partition by id order by date)
              else 0
         end)
        )
       ) /
       (1 +
        (case when lag(date, 1) over (partition by id order by date) >=
                   date - interval '2 day'
              then 1
              else 0
         end) +
        (case when lag(date, 2) over (partition by id order by date) >=
                   date - interval '2 day'
              then 1
              else 0
         end)
       )
from dataset t;

在许多或所有情况下,都可以使用以下方法代替“范围窗口”选项。 您可以为每个输入记录引入到期。到期记录将否定原始记录,因此当您聚合所有之前的记录时,将只考虑所需范围内的记录。 AVG有点难,因为它没有一个直接的对立面,所以我们需要将其视为求和/计数,并将两者都取反

选择id、日期、运行平均分数 从…起 选择id、日期、n、, 在无界的前一行和当前行之间按id顺序按日期行划分的SUMscore /在无界的前一行和当前行之间按id顺序按日期行划分的NULLIFSUMn,0作为运行的平均分数 从…起 选择日期、id、分数、1作为n 从数据集 联合所有 -失效与否定 选择DATEADDDAY,3,date,id,-1*分数,-1 从数据集 A. 其中a.n=1
编辑您的问题并显示您想要的结果。首先,谢谢!读你的书,读得好!我希望有一个动态的答案,在3天内不需要硬编码的解决方案。