窗口函数与日期时间的结合

问题描述 投票:0回答:3

窗口函数与日期时间的组合:

Snowflake中有类似的东西吗?:

select t.*,
       sum(qty) over (partition by article
                      order by date
                      range between interval '27 day' preceding and current row
                     ) as sum_qty_28_days
from t;

摘自:

使用sql窗口函数求最近n天的数量

snowflake-cloud-data-platform
3个回答
3
投票

来自 Snowflake 的 文档

对于滑动窗框:

ROWS 具有包容性,并且始终相对于当前行。

不支持范围

您最好的选择是您共享的链接中使用的相关子查询方法。


0
投票

RANGE
窗口函数中的子句现在可在 Snowflake 中使用。示例:

输入表:

文章 数量 获取日期
A 3 2019-10-11
A 5 2019-10-08
A 10 2019-10-05
A 2 2019-09-15
A 1 2019-09-09
A 1 2019-09-01
B 3 2019-10-11
B 2 2019-10-08
B 3 2019-10-05
B 1 2019-09-15
B 4 2019-09-09
C 1 2019-10-11
C 2 2019-10-08
C 1 2019-10-05
C 1 2019-09-15
C 0 2019-09-09
C 4 2019-09-01
C 1 2019-08-28

SQL 查询

select 
  *
  , sum(qty) over (partition by article
      order by date_acquired
      range between interval '28 day' preceding and current row
    ) as sum_qty_28_days
from acquisitions;

查询输出

文章 数量 获取日期 总数量_28_天
A 1 2019-09-01 1
A 1 2019-09-09 2
A 2 2019-09-15 4
A 10 2019-10-05 13
A 5 2019-10-08 17
A 3 2019-10-11 20
C 1 2019-08-28 1
C 4 2019-09-01 5
C 0 2019-09-09 5
C 1 2019-09-15 6
C 1 2019-10-05 2
C 2 2019-10-08 4
C 1 2019-10-11 5
B 4 2019-09-09 4
B 1 2019-09-15 5
B 3 2019-10-05 8
B 2 2019-10-08 6
B 3 2019-10-11 9

-1
投票

在雪花中,可以使用以下查询

select t.*,
       sum(qty) over (partition by article
                      order by date
                      rows between 27 preceding and current row
                     ) as sum_qty_28_days
from t;
© www.soinside.com 2019 - 2024. All rights reserved.