CUBE and ROLLUP
On this page
The CUBE and ROLLUP operations let you group by different subsets of a specified set of columns in a SELECT clause.
CUBE(column_ groups by all subsets of the columns specified in column_.CUBE(a, b) groups by (a), (b), (a, b) and () (which represents the entire input set of rows).ROLLUP(column_ groups by all subsets, including the entire set of input rows, (), then the leftmost column, the two leftmost columns, and so on.ROLLUP(a, b) groups by (), (a), and (a, b).
Syntax
Refer to the definition of the SELECT clause for where extended_ can be used.
extended_grouping_expr ::=
CUBE ( col_name | expr | position}, ...)
| ROLLUP ( col_name | expr | position}, ... )Remarks
Output rows for CUBE and ROLLUP contain a NULL value in the position of a grouping column if that row represents an aggregate result that does not group by that column.
Examples
The following table contains some hypothetical sales quantities for products sold by state.
CREATE TABLE cube_sales(state VARCHAR(30), product_id INT, quantity INT);INSERT cube_sales VALUES("Oregon", 1, 10), ("Washington", 1, 15), ("California", 1, 40),("Oregon", 2, 15), ("Washington", 2, 25), ("California", 2, 70);
This query uses CUBE to group by all subsets of columns in (state, product_:
SELECT state, product_id, SUM(quantity)FROM cube_salesGROUP BY CUBE(state, product_id)ORDER BY state, product_id;+------------+------------+---------------+| state | product_id | SUM(quantity) |+------------+------------+---------------+| NULL | NULL | 175 || NULL | 1 | 65 || NULL | 2 | 110 || California | NULL | 110 || California | 1 | 40 || California | 2 | 70 || Oregon | NULL | 25 || Oregon | 1 | 10 || Oregon | 2 | 15 || Washington | NULL | 40 || Washington | 1 | 15 || Washington | 2 | 25 |+------------+------------+---------------+
The first row above represents the grand total.
This query is the same as the one above but uses ROLLUP instead of CUBE:
SELECT state, product_id, SUM(quantity)FROM cube_salesGROUP BY ROLLUP(state, product_id)ORDER BY state, product_id;+------------+------------+---------------+| state | product_id | SUM(quantity) |+------------+------------+---------------+| NULL | NULL | 175 || California | NULL | 110 || California | 1 | 40 || California | 2 | 70 || Oregon | NULL | 25 || Oregon | 1 | 10 || Oregon | 2 | 15 || Washington | NULL | 40 || Washington | 1 | 15 || Washington | 2 | 25 |+------------+------------+---------------+
Notice that the results are the same as for CUBE except that there is no total for product_ 1 and product_ 2 across all states.
GROUPING() Function
The GROUPING(column_ can be used in the output column list of a SELECT block to show whether an output row of a query with a GROUP BY clause has aggregated together all the input rows for different values of column_.GROUPING(column_ is 1 if the result row has grouped together rows for column_ and 0 otherwise.
SELECT state, product_id, SUM(quantity), GROUPING(state), GROUPING(product_id)FROM cube_salesGROUP BY CUBE(state, product_id)ORDER BY state, product_id;+------------+------------+---------------+-----------------+----------------------+| state | product_id | SUM(quantity) | GROUPING(state) | GROUPING(product_id) |+------------+------------+---------------+-----------------+----------------------+| NULL | NULL | 175 | 1 | 1 || NULL | 1 | 65 | 1 | 0 || NULL | 2 | 110 | 1 | 0 || California | NULL | 110 | 0 | 1 || California | 1 | 40 | 0 | 0 || California | 2 | 70 | 0 | 0 || Oregon | NULL | 25 | 0 | 1 || Oregon | 1 | 10 | 0 | 0 || Oregon | 2 | 15 | 0 | 0 || Washington | NULL | 40 | 0 | 1 || Washington | 1 | 15 | 0 | 0 || Washington | 2 | 25 | 0 | 0 |+------------+------------+---------------+-----------------+----------------------+
The first row of output has GROUPING(state) = 1 and GROUPING(product_ because the row represents the grand total, meaning that rows for all state and product_ values have been combined together to form this row.GROUPING(state) = 1 but GROUPING(product_ because it represents a summary across all state values for a specific product_.
Last modified: February 28, 2023