Subtotals and Totals: ROLLUP, CUBE, GROUPING SETS
Reports need subtotals and grand totals along with detail rows. You can write a separate GROUP BY for each level and join them with UNION ALL, but the standard GROUPING SETS, ROLLUP, and CUBE aggregate several levels in one statement. All three DBs support the same syntax.
GROUPING SETS: listing the grouping criteria
GROUP BY GROUPING SETS ((a, b), (a), ()) returns aggregates by (a, b), by a, and over everything in one result. () is the whole set (grand total). Columns not in a grouping come out as NULL.
By grade and region, by grade, and overall
SELECT grade, region, COUNT(*) AS cnt
FROM customers
GROUP BY GROUPING SETS ((grade, region), (grade), ())
ORDER BY grade, region
The position of NULLs in the result differs by DB. In Oracle and PostgreSQL, subtotal rows come at the end of each group; in SQL Server, at the front (Sort position of NULL).
ROLLUP: removing one column at a time from the right
ROLLUP(a, b) is shorthand for GROUPING SETS ((a, b), (a), ()). Use it for subtotals and grand totals of hierarchical columns (year-month-day, grade-region).
ROLLUP(grade, region)
SELECT grade, region, COUNT(*) AS cnt
FROM customers
GROUP BY ROLLUP(grade, region)
ORDER BY grade, region
CUBE: every combination
CUBE(a, b) is GROUPING SETS ((a, b), (a), (b), ()). With n columns, it aggregates 2ⁿ combinations.
CUBE(grade, region)
SELECT grade, region, COUNT(*) AS cnt
FROM customers
GROUP BY CUBE(grade, region)
ORDER BY grade, region
Region subtotals (grade is NULL) appear along with grade subtotals (region is NULL).
GROUPING: identifying subtotal rows
A NULL in a subtotal row looks the same as a NULL in the original data. GROUPING(column) is 1 on rows where that column was aggregated away (subtotals) and 0 otherwise. Use it to attach labels and to fix the sort position regardless of DB.
Label subtotals and grand totals
SELECT CASE WHEN GROUPING(grade) = 1 THEN '(TOTAL)' ELSE grade END AS grade_label,
CASE WHEN GROUPING(region) = 1 THEN '(SUBTOTAL)' ELSE region END AS region_label,
COUNT(*) AS cnt
FROM customers
GROUP BY ROLLUP(grade, region)
ORDER BY GROUPING(grade), grade, GROUPING(region), region
GROUPING(…) in ORDER BY puts detail rows (0) first and subtotals and grand totals (1) last. This query returns the same order on all three DBs. The label aliases differ from the original column names (grade) because Oracle would read grade in ORDER BY as the alias, making GROUPING(grade) an aggregate function inside an aggregate function.
Applying it to order data
Order amounts by status for customers 101-103 with customer subtotals
SELECT cust_id, status, COUNT(*) AS orders, SUM(amt) AS total_amt,
GROUPING(cust_id) AS g_cust, GROUPING(status) AS g_status
FROM orders
WHERE cust_id BETWEEN 101 AND 103
GROUP BY ROLLUP(cust_id, status)
ORDER BY GROUPING(cust_id), cust_id, GROUPING(status), status
NOTE SQL Server and MySQL also have the older syntax
GROUP BY a, b WITH ROLLUP. The result is the same as the standardGROUP BY ROLLUP(a, b).
Pivoting row values into columns is covered in Rows to columns: pivot and unpivot.