ROLLUP, CUBE & GROUPING SETS
The problem
A report needs totals at several levels: per user, per city, and overall. Without these features you'd write three GROUP BY queries and combine them with UNION ALL.
GROUPING SETS – choose the levels you want
GROUP BY GROUPING SETS ((city, user_id), (city), ())
(city, user_id)→ detail rows(city)→ a subtotal per city()→ the grand total Columns not part of a set show as NULL in those rows.
ROLLUP – hierarchical subtotals
GROUP BY ROLLUP (city, user_id) = GROUPING SETS ((city, user_id), (city), ())
Perfect for hierarchies: year → month → day, country → city → store.
CUBE – every combination
GROUP BY CUBE (city, category) = all combinations: (city, category), (city), (category), ()
Useful for cross-tab analysis. With n columns you get 2ⁿ groupings, so use it carefully.
GROUPING() – tell subtotal rows apart
NULL can mean "subtotal row" or a real NULL value (like Hari's city).
GROUPING(city) returns 1 when the row is a subtotal over city, 0 for a normal value.
Use it to label rows: CASE WHEN GROUPING(city) = 1 THEN 'All cities' ELSE city END
Ordering
Sort so subtotals appear after their details:
ORDER BY city NULLS LAST, user_id NULLS LAST or sort by GROUPING(…)
Where you'll use this
Finance reports, sales dashboards, Excel-style summaries with subtotal rows
Example
-- ROLLUP: spending per category within each month, month subtotals, and a grand total
SELECT
CASE WHEN GROUPING(date_trunc('month', e.spent_on)) = 1 THEN 'ALL MONTHS'
ELSE to_char(date_trunc('month', e.spent_on), 'Mon YYYY') END AS month,
CASE WHEN GROUPING(c.name) = 1 THEN '— subtotal —' ELSE c.name END AS category,
sum(e.amount) AS total
FROM expenses e
JOIN categories c ON c.id = e.category_id
WHERE c.name IN ('Groceries', 'Transport')
GROUP BY ROLLUP (date_trunc('month', e.spent_on), c.name)
ORDER BY date_trunc('month', e.spent_on) NULLS LAST, GROUPING(c.name), c.name;
-- Sep 2026 | Groceries | 7300.00
-- Sep 2026 | Transport | 4650.00
-- Sep 2026 | — subtotal — | 11950.00
-- ...
-- ALL MONTHS | — subtotal — | 34970.00
-- GROUPING SETS: totals by city, totals by kind (essential/other), and a grand total
SELECT CASE WHEN GROUPING(city) = 1 THEN '(all cities)' ELSE COALESCE(city, 'Unknown') END AS city,
CASE WHEN GROUPING(kind) = 1 THEN '(all kinds)' ELSE kind END AS kind,
sum(amount) AS total
FROM (
SELECT u.city,
CASE WHEN 'essential' = ANY(e.tags) THEN 'essential' ELSE 'other' END AS kind,
e.amount
FROM expenses e JOIN users u ON u.id = e.user_id
) t
GROUP BY GROUPING SETS ((city), (kind), ())
ORDER BY GROUPING(city), GROUPING(kind), 1, 2;
-- Kathmandu | (all kinds) | 46040.00
-- Lalitpur | (all kinds) | 7940.00
-- Pokhara | (all kinds) | 22630.00
-- (all cities) | essential | 32360.00
-- (all cities) | other | 44250.00
-- (all cities) | (all kinds) | 76610.00
-- CUBE: every combination of city × size bucket
SELECT u.city,
CASE WHEN e.amount >= 3000 THEN 'big' ELSE 'small' END AS size,
count(*) AS n
FROM expenses e JOIN users u ON u.id = e.user_id
GROUP BY CUBE (u.city, CASE WHEN e.amount >= 3000 THEN 'big' ELSE 'small' END)
ORDER BY 1 NULLS LAST, 2 NULLS LAST;