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;
摘自:
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 |
在雪花中,可以使用以下查询
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;