Two SQLite rules for a RANGE offset
Two SQLite rules decide whether that frame works at all.
In SQLite, can a RANGE offset ORDER BY the date text itself?
The first rule: the ORDER BY has to be a number. Order by the text column itself, and nothing errors:
WITH daily AS (
SELECT ordered_on, COUNT(*) AS n
FROM orders
GROUP BY ordered_on
)
SELECT ordered_on, n,
SUM(n) OVER (ORDER BY ordered_on
RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS text_order,
SUM(n) OVER (ORDER BY julianday(ordered_on)
RANGE BETWEEN 6 PRECEDING AND CURRENT ROW) AS julian_order
FROM daily
ORDER BY ordered_on;| ordered_on | n | text_order | julian_order |
|---|---|---|---|
| 2026-03-01 | 2 | 2 | 2 |
| 2026-03-02 | 1 | 1 | 3 |
| 2026-03-03 | 2 | 2 | 5 |
| 2026-03-09 | 1 | 1 | 3 |
| 2026-03-10 | 1 | 1 | 2 |
The text version returns each date's own count. SQLite's rule: when the ORDER BY values aren't numbers, 6 PRECEDING reaches back only to rows with the same value as the current one. Here that quietly means this date only.
The second rule: a RANGE frame with an offset takes exactly one ORDER BY expression. Add a tie-break, and SQLite refuses with RANGE with offset PRECEDING/FOLLOWING requires one ORDER BY expression. You don't need one here. Rows on the same date are peers, and they belong in the frame together.
PostgreSQL can order by the date column itself, because its RANGE offset takes its type from the ordering column. From its docs: "For numeric ordering columns it is typically of the same type as the ordering column, but for datetime ordering columns it is an interval." There the same frame is ORDER BY ordered_on RANGE BETWEEN '6 days' PRECEDING AND CURRENT ROW on a date column.
In the editor, write ORDERS_3D: a 3-day RANGE frame over the day number.
- 1
Write
ORDERS_3D: for each date with orders,ordered_onandorders_3d, the orders in the 3 days ending on that date. - 2
Order the window by the day number,
julianday(ordered_on), with a RANGE frame.
Run your code to check it. Output shows here.