ROLLUP extensions
ROLLUP is a GROUP BY extension that produces hierarchical subtotals and a grand total in a single query. It generates one summary row for each prefix of the expression list, from the full combination down to the empty set (grand total).
The number of grouping levels or totals is N + 1, where N is the number of items in the ROLLUP expression list. A parenthesized sub-list counts as one item.
Syntax
SELECT select_list FROM ...
GROUP BY [... ,] ROLLUP ( { expr_1 | ( expr_1a [, expr_1b ] ...) }
[, expr_2 | ( expr_2a [, expr_2b ] ...) ] ...) [, ...]Expression list rules:
Each expression in the list defines one grouping level of the hierarchy. The leftmost expression is the top of the hierarchy; the rightmost is the bottom.
Enclose multiple expressions in parentheses —
(expr_1a, expr_1b)— to treat them as a single grouping level. This collapses what would be separate subtotal levels into one.Each item in the
select_listmust be one of: an expression from theROLLUPlist, an aggregate function (COUNT,SUM,AVG,MIN, orMAX), or a constant or function whose value is independent of individual rows (such asSYSDATE).
Output behavior:
The base rows reflect each unique combination of all expressions in the list.
A subtotal row is generated for each unique value of the first item, for each unique value of the second item within the first, and so on.
The final row is the grand total across the entire result set.
Subtotal rows display
NULLfor the columns that are rolled up.
Ordering: Without an ORDER BY clause, the order of the result set is not determined. Add ORDER BY to produce a meaningful hierarchical layout.
Examples
The examples in this section use the following tables. Run the setup script once before executing any example.
-- Create tables
CREATE TABLE dept (
deptno NUMBER(2),
dname VARCHAR2(14),
loc VARCHAR2(13)
);
CREATE TABLE emp (
empno NUMBER(4),
ename VARCHAR2(10),
job VARCHAR2(9),
deptno NUMBER(2)
);
-- Insert department data
INSERT INTO dept VALUES (10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept VALUES (20, 'RESEARCH', 'BOSTON');
INSERT INTO dept VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES (40, 'OPERATIONS', 'BOSTON');
-- Insert employee data (17 employees total)
INSERT INTO emp VALUES (7369, 'SMITH', 'CLERK', 20);
INSERT INTO emp VALUES (7499, 'ALLEN', 'SALESMAN', 30);
INSERT INTO emp VALUES (7521, 'WARD', 'SALESMAN', 30);
INSERT INTO emp VALUES (7566, 'JONES', 'MANAGER', 20);
INSERT INTO emp VALUES (7654, 'MARTIN', 'SALESMAN', 30);
INSERT INTO emp VALUES (7698, 'BLAKE', 'MANAGER', 30);
INSERT INTO emp VALUES (7782, 'CLARK', 'MANAGER', 10);
INSERT INTO emp VALUES (7788, 'SCOTT', 'ANALYST', 20);
INSERT INTO emp VALUES (7839, 'KING', 'PRESIDENT',10);
INSERT INTO emp VALUES (7844, 'TURNER', 'SALESMAN', 30);
INSERT INTO emp VALUES (7876, 'ADAMS', 'CLERK', 20);
INSERT INTO emp VALUES (7900, 'JAMES', 'CLERK', 30);
INSERT INTO emp VALUES (7902, 'FORD', 'ANALYST', 20);
INSERT INTO emp VALUES (7934, 'MILLER', 'CLERK', 10);
INSERT INTO emp VALUES (7940, 'BATES', 'ANALYST', 40);
INSERT INTO emp VALUES (7941, 'COOPER', 'CLERK', 40);
INSERT INTO emp VALUES (7942, 'HAYES', 'MANAGER', 40);Three-level rollup
This query rolls up over loc, dname, and job — three items, producing four subtotal levels (N + 1 = 4).
SELECT loc, dname, job, COUNT(*) AS "employees"
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY ROLLUP (loc, dname, job)
ORDER BY 1, 2, 3;The result includes base rows, a subtotal for each loc+dname combination, a subtotal for each loc, and a grand total:
| loc | dname | job | employees |
|---|---|---|---|
| BOSTON | OPERATIONS | ANALYST | 1 |
| BOSTON | OPERATIONS | CLERK | 1 |
| BOSTON | OPERATIONS | MANAGER | 1 |
| BOSTON | OPERATIONS | 3 | |
| BOSTON | RESEARCH | ANALYST | 2 |
| BOSTON | RESEARCH | CLERK | 2 |
| BOSTON | RESEARCH | MANAGER | 1 |
| BOSTON | RESEARCH | 5 | |
| BOSTON | 8 | ||
| CHICAGO | SALES | CLERK | 1 |
| CHICAGO | SALES | MANAGER | 1 |
| CHICAGO | SALES | SALESMAN | 4 |
| CHICAGO | SALES | 6 | |
| CHICAGO | 6 | ||
| NEW YORK | ACCOUNTING | CLERK | 1 |
| NEW YORK | ACCOUNTING | MANAGER | 1 |
| NEW YORK | ACCOUNTING | PRESIDENT | 1 |
| NEW YORK | ACCOUNTING | 3 | |
| NEW YORK | 3 | ||
| 17 |
Grouped expressions: collapsing subtotal levels
Enclosing two expressions in parentheses treats them as a single item, which reduces the number of subtotal levels.
The following query groups (dname, job) as one item, giving two items total and three subtotal levels:
SELECT loc, dname, job, COUNT(*) AS "employees"
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY ROLLUP (loc, (dname, job))
ORDER BY 1, 2, 3;Compared to the three-level rollup, the subtotals for each loc+dname combination are gone. Only loc subtotals and the grand total remain:
| loc | dname | job | employees |
|---|---|---|---|
| BOSTON | OPERATIONS | ANALYST | 1 |
| BOSTON | OPERATIONS | CLERK | 1 |
| BOSTON | OPERATIONS | MANAGER | 1 |
| BOSTON | RESEARCH | ANALYST | 2 |
| BOSTON | RESEARCH | CLERK | 2 |
| BOSTON | RESEARCH | MANAGER | 1 |
| BOSTON | 8 | ||
| CHICAGO | SALES | CLERK | 1 |
| CHICAGO | SALES | MANAGER | 1 |
| CHICAGO | SALES | SALESMAN | 4 |
| CHICAGO | 6 | ||
| NEW YORK | ACCOUNTING | CLERK | 1 |
| NEW YORK | ACCOUNTING | MANAGER | 1 |
| NEW YORK | ACCOUNTING | PRESIDENT | 1 |
| NEW YORK | 3 | ||
| 17 |
The following query groups (loc, dname) as the first item instead:
SELECT loc, dname, job, COUNT(*) AS "employees"
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY ROLLUP ((loc, dname), job)
ORDER BY 1, 2, 3;This produces a subtotal for each unique loc+dname combination, but no subtotal for loc alone:
| loc | dname | job | employees |
|---|---|---|---|
| BOSTON | OPERATIONS | ANALYST | 1 |
| BOSTON | OPERATIONS | CLERK | 1 |
| BOSTON | OPERATIONS | MANAGER | 1 |
| BOSTON | OPERATIONS | 3 | |
| BOSTON | RESEARCH | ANALYST | 2 |
| BOSTON | RESEARCH | CLERK | 2 |
| BOSTON | RESEARCH | MANAGER | 1 |
| BOSTON | RESEARCH | 5 | |
| CHICAGO | SALES | CLERK | 1 |
| CHICAGO | SALES | MANAGER | 1 |
| CHICAGO | SALES | SALESMAN | 4 |
| CHICAGO | SALES | 6 | |
| NEW YORK | ACCOUNTING | CLERK | 1 |
| NEW YORK | ACCOUNTING | MANAGER | 1 |
| NEW YORK | ACCOUNTING | PRESIDENT | 1 |
| NEW YORK | ACCOUNTING | 3 | |
| 17 |