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_onorders_7d
2026-03-012
2026-03-023
2026-03-035
2026-03-093
2026-03-102

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.