18/34
Part 4 of 6 · FramesLearn and do

ROWS counts rows, and RANGE measures the ORDER BY value

A frame has a unit and two ends. The unit decides what PRECEDING and FOLLOWING count:

  • ROWS counts rows. 6 PRECEDING reaches back six rows, whatever dates they hold.
  • RANGE measures the ORDER BY value. 6 PRECEDING reaches back to every row whose value is at most 6 below the current row's.
  • GROUPS counts peer groups. 1 PRECEDING reaches back one group, so the frame holds the current row's peers and the set of peers just before them.

Here's the difference made visible. The query counts orders per date. Then for each date it sums the counts inside each frame, and lists which days of March the frame holds, with group_concat:

WITH daily AS (
    SELECT ordered_on, COUNT(*) AS n
    FROM orders
    GROUP BY ordered_on
)
SELECT ordered_on, n,
       group_concat(substr(ordered_on, 9), ' ') OVER by_rows AS rows_frame,
       SUM(n) OVER by_rows AS rows_sum,
       group_concat(substr(ordered_on, 9), ' ') OVER by_days AS range_frame,
       SUM(n) OVER by_days AS range_sum
FROM daily
WINDOW by_rows AS (ORDER BY julianday(ordered_on)
                   ROWS BETWEEN 6 PRECEDING AND CURRENT ROW),
       by_days AS (ORDER BY julianday(ordered_on)
                   RANGE BETWEEN 6 PRECEDING AND CURRENT ROW)
ORDER BY ordered_on;
ordered_onnrows_framerows_sumrange_framerange_sum
2026-03-012012012
2026-03-02101 02301 023
2026-03-03201 02 03501 02 035
2026-03-09101 02 03 09603 093
2026-03-10101 02 03 09 10709 102

Through March 3 they agree, because the first three dates are consecutive. March 9 is where they split. On a calendar:

6 PRECEDING for the row dated March 9
ROWS the row and the 6 before it reach back to March 1
RANGE day numbers within 6 of March 9: March 3 to 9
12345678910
day of March

The dashed lines mark the days that have a row.

The ROWS frame reaches back to March 1. It holds the current row and up to six before it, and only three rows come before it. The RANGE frame stops at March 3, six days back.

With gaps between dates, ROWS covers more than a week. With several rows per date, it covers less. Only RANGE over a day number means the last 7 days.

In the editor, write LAST_THREE_DATES: a ROWS frame on purpose, the orders over the last three dates that had any.