ROLLUP, CUBE and GROUPING SETS
Overview
Multi-level reports combine detailed groups, subtotals and a grand total. PostgreSQL 17 supports GROUPING SETS for choosing levels, ROLLUP for prefixes of a dimension list, and CUBE for all subsets. SQLite 3.46.1 has no native syntax for these constructs; use separate grouped queries joined with UNION ALL for this fixed report.
First example
SELECT region, channel, 0 AS mask, SUM(amount_cents) AS total FROM grouping_sales GROUP BY region, channel UNION ALL SELECT region, NULL, 1, SUM(amount_cents) FROM grouping_sales GROUP BY region UNION ALL SELECT NULL, NULL, 3, SUM(amount_cents) FROM grouping_sales ORDER BY mask, region NULLS FIRST, channel NULLS FIRST;Observed SQLite result (rows in JSON):
[ [ null, "online", 0, 40 ], [ "North", "online", 0, 150 ], [ "North", "store", 0, 200 ], [ "South", null, 0, 20 ], [ "South", "online", 0, 80 ], [ null, null, 1, 40 ], [ "North", null, 1, 350 ], [ "South", null, 1, 100 ], [ null, null, 3, 490 ]]How it works
For (region, channel), ROLLUP creates (region,channel), (region), and (). CUBE also includes (channel). GROUPING(region,channel) returns a mask: 0 detail, 1 region subtotal, 2 channel subtotal, 3 grand total. The rightmost argument is the low bit. A real missing region or channel is not a subtotal. Our first SQLite example explicitly assigns masks 0,1,3; the second demonstrates masks 2,3. UNION ALL appends result rows without duplicate elimination (set operations are taught later). This emulation is for these simple SUM reports, not a universal optimizer-equivalent rewrite.
Practical example
SELECT NULL AS region, channel, 2 AS mask, SUM(amount_cents) AS total FROM grouping_sales GROUP BY channel UNION ALL SELECT NULL, NULL, 3, SUM(amount_cents) FROM grouping_sales ORDER BY mask, channel NULLS FIRST;Observed SQLite result (rows in JSON):
[ [ null, null, 2, 20 ], [ null, "online", 2, 270 ], [ null, "store", 2, 200 ], [ null, null, 3, 490 ]]Practice and mistakes
Practice: identify the row with mask 1 and NULL region (40 cents) versus mask 3 and NULL region (490 cents). Never sum detail and subtotal rows together. More grouping sets may increase work; inspect plans and measure on representative data rather than assuming a speedup.
PostgreSQL 17 — not executed
SELECT region, channel, GROUPING(region,channel) AS mask, SUM(amount_cents) AS total FROM grouping_sales GROUP BY ROLLUP(region,channel) ORDER BY mask, region NULLS FIRST, channel NULLS FIRST;Expected result: [[null, "online", 0, 40], ["North", "online", 0, 150], ["North", "store", 0, 200], ["South", null, 0, 20], ["South", "online", 0, 80], [null, null, 1, 40], ["North", null, 1, 350], ["South", null, 1, 100], [null, null, 3, 490]]
PostgreSQL 17 — not executed
SELECT region, channel, GROUPING(region,channel) AS mask, SUM(amount_cents) AS total FROM grouping_sales GROUP BY CUBE(region,channel) ORDER BY mask, region NULLS FIRST, channel NULLS FIRST;Expected result: [[null, "online", 0, 40], ["North", "online", 0, 150], ["North", "store", 0, 200], ["South", null, 0, 20], ["South", "online", 0, 80], [null, null, 1, 40], ["North", null, 1, 350], ["South", null, 1, 100], [null, null, 2, 20], [null, "online", 2, 270], [null, "store", 2, 200], [null, null, 3, 490]]
PostgreSQL 17 — not executed
SELECT region, channel, GROUPING(region,channel) AS mask, SUM(amount_cents) AS total FROM grouping_sales GROUP BY GROUPING SETS ((region,channel),(region),()) ORDER BY mask, region NULLS FIRST, channel NULLS FIRST;Expected result: [[null, "online", 0, 40], ["North", "online", 0, 150], ["North", "store", 0, 200], ["South", null, 0, 20], ["South", "online", 0, 80], [null, null, 1, 40], ["North", null, 1, 350], ["South", null, 1, 100], [null, null, 3, 490]]