Sub topic 12
Subtotals with ROLLUP, CUBE and GROUPING SETS
This chapter assumes you know SELECT, WHERE, GROUP BY, HAVING, and basic joins. It also assumes that orders has customer_id, status, and amount columns.
A normal grouped query returns one row for each group. Sometimes a report also needs subtotals and a grand total. SQL provides ROLLUP, CUBE, and GROUPING SETS for this purpose.
ROLLUP for hierarchical subtotals
ROLLUP creates subtotals by removing grouping columns from right to left. For example, this query groups first by customer and then by status:
SELECT customer_id,
status,
SUM(amount) AS total_amount
FROM orders
GROUP BY ROLLUP (customer_id, status)
ORDER BY customer_id, status;The result contains:
- One row for each
customer_idandstatuscombination. - One subtotal row for each
customer_id. - One grand total row for all orders.
The subtotal and grand total rows contain NULL in columns that have been rolled up. In this example, a row with a customer ID and NULL status means “all statuses for this customer.”
The order of columns matters. ROLLUP (customer_id, status) creates customer subtotals, while ROLLUP (status, customer_id) creates status subtotals instead.
Distinguishing subtotal NULLs
A real NULL value in status can mean “unknown status.” A generated NULL can mean “this row is a subtotal.” The GROUPING function distinguishes them.
SELECT customer_id,
status,
SUM(amount) AS total_amount,
GROUPING(customer_id) AS customer_is_rolled_up,
GROUPING(status) AS status_is_rolled_up
FROM orders
GROUP BY ROLLUP (customer_id, status);GROUPING(column) returns 1 when the column was removed to create a subtotal and 0 when it represents an actual grouping value. You can use this result to label report rows safely.
SELECT CASE
WHEN GROUPING(customer_id) = 1 THEN 'Grand total'
WHEN GROUPING(status) = 1 THEN 'Customer subtotal'
ELSE 'Detail'
END AS row_type,
customer_id,
status,
SUM(amount) AS total_amount
FROM orders
GROUP BY ROLLUP (customer_id, status);CUBE for every combination
CUBE creates subtotals for every combination of the listed grouping columns.
SELECT customer_id,
status,
SUM(amount) AS total_amount
FROM orders
GROUP BY CUBE (customer_id, status);With two columns, this includes:
- Customer and status detail rows.
- Customer subtotals across all statuses.
- Status subtotals across all customers.
- One grand total.
CUBE is useful for multidimensional reports, but it can produce many rows. With n grouping columns, it can produce up to 2^n grouping combinations.
GROUPING SETS for selected summaries
GROUPING SETS lets you name exactly which grouping levels you need.
SELECT customer_id,
status,
SUM(amount) AS total_amount
FROM orders
GROUP BY GROUPING SETS (
(customer_id, status),
(customer_id),
(status),
()
);Here, () means the grand total. This query requests the same levels as CUBE (customer_id, status), but you can omit any level that the report does not need.
| Feature | Creates | Best use |
|---|---|---|
| `ROLLUP` | Hierarchical subtotals and a grand total | Reports with ordered levels |
| `CUBE` | Every combination of grouping columns | Full multidimensional analysis |
| `GROUPING SETS` | Only the grouping sets you list | Precise control over report rows |
Database syntax varies. PostgreSQL and several other systems support the syntax shown here. MySQL commonly writes GROUP BY customer_id, status WITH ROLLUP for a rollup query, and support for CUBE or GROUPING SETS depends on the version and database system.
Exercise
You need detail rows by customer and status, customer subtotals, but no status-only subtotals. Which grouping clause should you use?
Show answer
Use GROUPING SETS: GROUP BY GROUPING SETS ((customerid, status), (customerid), ()).