22/34
Part 4 of 6 · FramesLearn and do
DISTINCT over the raw rows gives the same counts with no CTE
Since peers share a frame, you can skip the CTE. Run the window over the raw rows, where every order on a date gets the same count. Then collapse the repeats with DISTINCT, which runs after the windows:
SELECT DISTINCT ordered_on,
COUNT(*) OVER (ORDER BY julianday(ordered_on)
RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS orders_7d
FROM orders
ORDER BY ordered_on;
| ordered_on | orders_7d |
|---|---|
| 2026-03-01 | 2 |
| 2026-03-02 | 3 |
| 2026-03-03 | 5 |
| 2026-03-09 | 3 |
| 2026-03-10 | 2 |
Both are correct, and the CTE version is easier to read.
In the editor, finish REVENUE_2D: the store's revenue over the 2 days ending on each date.