Trending

World technology news

→
SQLite 3.28 GROUPS and EXCLUDE: calculating the previous order day without collapsing rows
Software Engineering · 2019
Software Engineering2019

SQLite 3.28 GROUPS and EXCLUDE: calculating the previous order day without collapsing rows

A runnable SQLite example keeps every order row while attaching the total for the previous observed order day, including repeated dates, a skipped calendar day, and an empty first frame.

Archive retrospective · Topic year 2019

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. The prior row is the wrong unit
  2. What SQLite 3.28 added in 2019
  3. Run the five-row fixture
  4. Missing calendar days and empty frames
  5. Partition by warehouse before comparing dates
  6. Choose detail or aggregate output deliberately
  7. Avoid double-counting a repeated comparison value

The prior row is the wrong unit

A warehouse report must preserve every order row. Next to two orders on April 16, the operator wants the total units from the previous day on which an order was recorded. April 15 has orders for three and seven units, so both April 16 rows should display ten. Selecting the preceding row can produce seven on one result and four on another. The discrepancy might look plausible until someone checks the original orders. Write down the intended grouping key and the precise meaning of previous before choosing a SQL expression. This is particularly useful when a report retains detail and adds a value at a different level of granularity.

What SQLite 3.28 added in 2019

The April 16, 2019 release notes for SQLite 3.28.0 identify GROUPS frames and EXCLUDE support as extensions to window functions. A GROUPS frame counts sets of peers according to the window ordering. The example orders its group frame by order_day, making orders on one date peers. The frame starts one group before the current group and ends at the current group. EXCLUDE GROUP removes the current peer group before the aggregate is evaluated. That leaves all the rows of the previous observed date. The window ordering and the final result ordering are separate. The former establishes which rows are peers, while the latter can put orders in a stable display sequence.

Database access flowConceptual sketch · summary of the article's technical flow

Run the five-row fixture

The query computes two columns over the same data. The comparison column using ROWS orders by day and order ID and reads just the previous row. The GROUPS column intentionally omits order ID from its window ordering. Running this query on SQLite 3.53.3, as bundled in the local Node.js 24.19.0 runtime, returned NULL, NULL, 10, 10 and 10 for the group calculation. The row calculation returned NULL, 3, 7, 4 and 6. The results verify the difference in the current runtime. The 2019 release date comes from the project’s release notes. In another environment, record SELECT sqlite_version() so the execution engine used for the fixture is explicit.

WITH orders(order_id, order_day, units) AS (
  VALUES (1,'2019-04-15',3), (2,'2019-04-15',7),
         (3,'2019-04-16',4), (4,'2019-04-16',6),
         (5,'2019-04-18',8)
)
SELECT order_id, order_day, units,
       sum(units) OVER (
         ORDER BY order_day
         GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE GROUP
       ) AS previous_order_day_units,
       sum(units) OVER (
         ORDER BY order_day, order_id
         ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING
       ) AS previous_row_units
FROM orders
ORDER BY order_day, order_id;

Missing calendar days and empty frames

There are no orders on April 17. The April 18 row therefore displays the April 16 total, because April 16 is the previous group present in the sequence. If the application needs yesterday's total, construct a calendar and represent April 17 explicitly. The difference matters when a warehouse closes for a holiday or its import fails. The first recorded date has no prior group at all. SUM over that empty frame returns NULL. Preserve the distinction between no observation and an observed total of zero if it affects downstream decisions. Convert the displayed value to zero only when that interpretation follows the report's stated rules.

Partition by warehouse before comparing dates

The fixture contains one warehouse. For a multi-warehouse report use an appropriate PARTITION BY warehouse identifier to compute group totals independently. Store or derive the business date consistently. A UTC order near midnight can fall on a different business date for warehouses in different time zones. A small integration test can include two warehouses, repeated dates and a midnight boundary. Where the business date is an ISO calendar string, validate that imports use the same format before relying on lexical order. Avoid adding a unique order ID to the GROUPS window ordering just to stabilize presentation. That belongs in the outer ordering if equal dates are supposed to remain peers.

Choose detail or aggregate output deliberately

A report that displays only daily totals can start with GROUP BY and produce one row per observed date. A detail report needs its original order rows, which is where a window aggregate helps. For an older SQLite deployment, a grouped daily summary joined back to the detail rows is an implementable alternative. Compare the outcome against the same fixture if maintaining both paths. Test performance on representative data separately. The small query demonstrates the semantics, not the cost of a workload on a particular database. Confirm that every order row is still present and that the column label explains which previous day is represented.

Avoid double-counting a repeated comparison value

The prior-day total is repeated on each order of a date. Do not sum this comparison column across rows to construct a second daily report. First reduce the results to one row per warehouse and order date, or compute the daily report directly from the grouped source. Label exports with the same wording as the interface and define how a missing prior date is represented. These checks make it possible to trace a summary number back to the query instead of treating repeated totals as independent observations.

761 reads ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.