GROUPING function

Updated at:

The GROUPING function identifies subtotal rows generated by ROLLUP, CUBE, or GROUPING SETS in a GROUP BY query. It returns 1 for a subtotal row and 0 for a base aggregate row, letting you filter or label aggregation levels in your result set.

Syntax

SELECT [ expr ...,] GROUPING( col_expr ) [, expr ] ...
FROM ...
GROUP BY [...,]
  { ROLLUP | CUBE | GROUPING SETS }( [...,] col_expr
  [, ...] ) [, ...]

Parameter: col_expr must be a dimension column expression from the ROLLUP, CUBE, or GROUPING SETS list in the GROUP BY clause.

Return values

ValueMeaning
1The row is a subtotal across multiple values of the specified column. The column appears null because it aggregates over all values.
0The row is a base aggregate for a specific value of the column. This includes rows where the column's actual data value is null.

Examples

Identify subtotals with ROLLUP

The following query uses ROLLUP on three columns and adds a GROUPING indicator column for each to mark subtotal rows.

SELECT loc, dname, job, COUNT(*) AS "employees",
  GROUPING(loc) AS "gf_loc",
  GROUPING(dname) AS "gf_dname",
  GROUPING(job) AS "gf_job"
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY ROLLUP (loc, dname, job)
ORDER BY 1, 2, 3;

In the result, gf_loc, gf_dname, and gf_job show 1 wherever the corresponding column is rolled up into a subtotal:

   loc    |   dname    |    job    | employees | gf_loc | gf_dname | gf_job
----------+------------+-----------+-----------+--------+----------+--------
 BOSTON   | OPERATIONS | ANALYST   |         1 |      0 |        0 |      0
 BOSTON   | OPERATIONS | CLERK     |         1 |      0 |        0 |      0
 BOSTON   | OPERATIONS | MANAGER   |         1 |      0 |        0 |      0
 BOSTON   | OPERATIONS |           |         3 |      0 |        0 |      1
 BOSTON   | RESEARCH   | ANALYST   |         2 |      0 |        0 |      0
 BOSTON   | RESEARCH   | CLERK     |         2 |      0 |        0 |      0
 BOSTON   | RESEARCH   | MANAGER   |         1 |      0 |        0 |      0
 BOSTON   | RESEARCH   |           |         5 |      0 |        0 |      1
 BOSTON   |            |           |         8 |      0 |        1 |      1
 CHICAGO  | SALES      | CLERK     |         1 |      0 |        0 |      0
 CHICAGO  | SALES      | MANAGER   |         1 |      0 |        0 |      0
 CHICAGO  | SALES      | SALESMAN  |         4 |      0 |        0 |      0
 CHICAGO  | SALES      |           |         6 |      0 |        0 |      1
 CHICAGO  |            |           |         6 |      0 |        1 |      1
 NEW YORK | ACCOUNTING | CLERK     |         1 |      0 |        0 |      0
 NEW YORK | ACCOUNTING | MANAGER   |         1 |      0 |        0 |      0
 NEW YORK | ACCOUNTING | PRESIDENT |         1 |      0 |        0 |      0
 NEW YORK | ACCOUNTING |           |         3 |      0 |        0 |      1
 NEW YORK |            |           |         3 |      0 |        1 |      1
          |            |           |        17 |      1 |        1 |      1
(20 rows)

Filter specific subtotal levels with HAVING

Use GROUPING in a HAVING clause to return only specific aggregation levels. The following query returns subtotals per location and department, excluding grand total and per-job detail rows:

SELECT loc, dname, job, COUNT(*) AS "employees",
  GROUPING(loc) AS "gf_loc",
  GROUPING(dname) AS "gf_dname",
  GROUPING(job) AS "gf_job"
FROM emp e, dept d
WHERE e.deptno = d.deptno
GROUP BY ROLLUP (loc, dname, job)
HAVING GROUPING(loc) = 0
  AND  GROUPING(dname) = 0
  AND  GROUPING(job) = 1
ORDER BY 1, 2;

Result:

   loc    |   dname    | job | employees | gf_loc | gf_dname | gf_job
----------+------------+-----+-----------+--------+----------+--------
 BOSTON   | OPERATIONS |     |         3 |      0 |        0 |      1
 BOSTON   | RESEARCH   |     |         5 |      0 |        0 |      1
 CHICAGO  | SALES      |     |         6 |      0 |        0 |      1
 NEW YORK | ACCOUNTING |     |         3 |      0 |        0 |      1
(4 rows)

Distinguish data nulls from subtotal nulls

A column can appear null for two different reasons: the underlying data contains a null value, or ROLLUP/CUBE generated a subtotal across all values. GROUPING tells them apart.

Insert a row with a null job value:

INSERT INTO emp (empno,ename,deptno) VALUES (9004,'PETERS',40);

Run a CUBE query filtered to Boston:

SELECT loc, job, COUNT(*) AS "employees",
  GROUPING(loc) AS "gf_loc",
  GROUPING(job) AS "gf_job"
FROM emp e, dept d
WHERE e.deptno = d.deptno AND loc = 'BOSTON'
GROUP BY CUBE (loc, job)
ORDER BY 1, 2;

Result:

  loc   |   job   | employees | gf_loc | gf_job
--------+---------+-----------+--------+--------
 BOSTON | ANALYST |         3 |      0 |      0
 BOSTON | CLERK   |         3 |      0 |      0
 BOSTON | MANAGER |         2 |      0 |      0
 BOSTON |         |         1 |      0 |      0
 BOSTON |         |         9 |      0 |      1
        | ANALYST |         3 |      1 |      0
        | CLERK   |         3 |      1 |      0
        | MANAGER |         2 |      1 |      0
        |         |         1 |      1 |      0
        |         |         9 |      1 |      1
(10 rows)

Two rows show BOSTON with a blank job (rows 4 and 5). GROUPING distinguishes them:

  • Row 4 (gf_job = 0, employees = 1): a base aggregate for the inserted row where job is actually null in the data.

  • Row 5 (gf_job = 1, employees = 9): a subtotal across all jobs in Boston generated by CUBE.

Row 9 (gf_loc = 1, gf_job = 0, employees = 1) is a subtotal across all locations where job is null in the data — not a subtotal across jobs.