Combination of window function with datetime

Viewed 859

Combination of window function with datetime:

Is there is something like this in 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;

taken from:

sum last n days quantity using sql window function

2 Answers

From Snowflake's documentation

For sliding window frames:

ROWS is inclusive and is always relative to the current row.

RANGE is not supported.

Your best bet is a correlated subquery approach used in the link you shared.

In snowflake, you can use the following query

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;
Related