Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GROUPING SETS, ROLLUP, and CUBE

Amphi supports grouping sets in aggregate queries:

SELECT region, store, sum(amount)
FROM sales
GROUP BY ROLLUP (region, store);

The following forms are also supported:

GROUP BY GROUPING SETS ((region), (store), ())
GROUP BY CUBE (region, store)
GROUP BY ROLLUP (region), CUBE (store, channel)

ROLLUP produces detail groups, progressive subtotals, and the grand total. CUBE produces every combination of the expressions. Expressions omitted from a set are returned as NULL. HAVING, ORDER BY, and existing aggregates use the normal aggregation path.

GROUPING() and GROUPING_ID() return an integer mask. The least significant bit corresponds to the last argument; a 1 bit means that the expression was omitted from the current grouping set. Multiple ROLLUP/CUBE components can be combined in one clause. Hash aggregate spill remains available when group cardinality exceeds work_mem. DISTINCT inside an aggregate combined with grouping sets is rejected in this slice.