ROLLUP extension v16
ROLLUP extension produces a hierarchical set of groups with subtotals for each hierarchical group as well as a grand total. The order of the hierarchy is determined by the order of the expressions given in the
ROLLUP expression list. The top of the hierarchy is the left-most item in the list. Each successive item proceeding to the right moves down the hierarchy. The right-most item is at the lowest level.
The syntax for a single
expr is an expression that determines the grouping of the result set. If enclosed in parenthesis as
( expr_1a, expr_1b, ...), then the combination of values returned by
expr_1b defines a single grouping level of the hierarchy.
The base level of aggregates returned in the result set is for each unique combination of values returned by the expression list.
In addition, a subtotal is returned for the first item in the list (
expr_1 or the combination of
( expr_1a, expr_1b, ...), whichever is specified) for each unique value. A subtotal is returned for the second item in the list (
expr_2 or the combination of
( expr_2a, expr_2b, ...), whichever is specified) for each unique value in each grouping of the first item and so on. Finally, a grand total is returned for the entire result set.
For the subtotal rows, null is returned for the items across which the subtotal is taken.
ROLLUP extension specified in the context of the
GROUP BY clause is shown by the following:
The items specified in
select_list must either:
- Also appear in the
- Be aggregate functions such as
- Be constants or functions whose return values are independent of the individual rows in the group (for example, the
GROUP BY clause to specify multiple
ROLLUP extensions as well as multiple occurrences of other
GROUP BY extensions and individual expressions.
ORDER BY clause to display the output in a hierarchical or other meaningful structure. Using this clause guarantees the order of the result set.
The number of grouping levels or totals is n + 1 where n represents the number of items in the
ROLLUP expression list. A parenthesized list counts as one item.
The following query produces a rollup based on a hierarchy of columns
dname, and then
The result of the query displays a count of the number of employees for each unique combination of
job, as well as subtotals for each unique combination of
dname for each unique value of
loc. A grand total appears on the last line:
The following query shows the effect of combining items in the
ROLLUP list in parentheses:
The output doesn't include subtotals for
dname combinations as in the prior example:
If the first two columns in the
ROLLUP list are enclosed in parentheses, the subtotal levels differ as well:
Now there's a subtotal for each unique
dname combination but none for unique values of