SQL Moving Window Frames
A frame that travels WITH the current row, instead of anchoring at the start of the partition.
What & Why
Replace UNBOUNDED PRECEDING with a fixed number, and the frame stops growing — it slides forward with each row instead, always covering the same span. This is the mechanism behind every moving average.
See How It Works
BUSINESS QUESTION
Growth wants a seven-row rolling active-user total.
| date_day | dau | new_users | returning_users |
|---|---|---|---|
| 2024-04-01 | 1240 | 180 | 1060 |
| 2024-04-02 | 1315 | 205 | 1110 |
| 2024-04-03 | 1288 | 164 | 1124 |
| 2024-04-04 | 1392 | 221 | 1171 |
EXAMPLE QUERY
SELECT
date_day,
dau,
SUM(dau) OVER (ORDER BY date_day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS rolling_7_row_dau
FROM growth.daily_active_users
ORDER BY date_day;RESULT — moving DAU sum
| date_day | dau | rolling_7_row_dau |
|---|---|---|
| 2024-04-01 | 1240 | 1240 |
| 2024-04-02 | 1315 | 2555 |
| 2024-04-03 | 1288 | 3843 |
| 2024-04-04 | 1392 | 5235 |
The trailing frame expands across the four available representative rows.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario