ROLLUP extensions

Updated at:

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_list must be one of: an expression from the ROLLUP list, an aggregate function (COUNT, SUM, AVG, MIN, or MAX), or a constant or function whose value is independent of individual rows (such as SYSDATE).

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 NULL for 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:

locdnamejobemployees
BOSTONOPERATIONSANALYST1
BOSTONOPERATIONSCLERK1
BOSTONOPERATIONSMANAGER1
BOSTONOPERATIONS3
BOSTONRESEARCHANALYST2
BOSTONRESEARCHCLERK2
BOSTONRESEARCHMANAGER1
BOSTONRESEARCH5
BOSTON8
CHICAGOSALESCLERK1
CHICAGOSALESMANAGER1
CHICAGOSALESSALESMAN4
CHICAGOSALES6
CHICAGO6
NEW YORKACCOUNTINGCLERK1
NEW YORKACCOUNTINGMANAGER1
NEW YORKACCOUNTINGPRESIDENT1
NEW YORKACCOUNTING3
NEW YORK3
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:

locdnamejobemployees
BOSTONOPERATIONSANALYST1
BOSTONOPERATIONSCLERK1
BOSTONOPERATIONSMANAGER1
BOSTONRESEARCHANALYST2
BOSTONRESEARCHCLERK2
BOSTONRESEARCHMANAGER1
BOSTON8
CHICAGOSALESCLERK1
CHICAGOSALESMANAGER1
CHICAGOSALESSALESMAN4
CHICAGO6
NEW YORKACCOUNTINGCLERK1
NEW YORKACCOUNTINGMANAGER1
NEW YORKACCOUNTINGPRESIDENT1
NEW YORK3
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:

locdnamejobemployees
BOSTONOPERATIONSANALYST1
BOSTONOPERATIONSCLERK1
BOSTONOPERATIONSMANAGER1
BOSTONOPERATIONS3
BOSTONRESEARCHANALYST2
BOSTONRESEARCHCLERK2
BOSTONRESEARCHMANAGER1
BOSTONRESEARCH5
CHICAGOSALESCLERK1
CHICAGOSALESMANAGER1
CHICAGOSALESSALESMAN4
CHICAGOSALES6
NEW YORKACCOUNTINGCLERK1
NEW YORKACCOUNTINGMANAGER1
NEW YORKACCOUNTINGPRESIDENT1
NEW YORKACCOUNTING3
17