GROUPING function v17
When using the ROLLUP
, CUBE
, or GROUPING SETS
extensions to the GROUP BY
clause, it can sometimes be difficult to differentiate between the various levels of subtotals generated by the extensions as well as the base aggregate rows in the result set. The GROUPING
function provides a means of making this distinction.
Syntax
The general syntax for use of the GROUPING
function is:
The GROUPING
function takes a single parameter that must be an expression of a dimension column specified in the expression list of a ROLLUP
, CUBE
, or GROUPING SETS
extension of the GROUP BY
clause.
The return value of the GROUPING
function is either a 0 or 1. In the result set of a query:
- If the column expression specified in the
GROUPING
function is null because the row represents a subtotal over multiple values of that column, then theGROUPING
function returns a value of 1. - If the row returns results based on a particular value of the column specified in the
GROUPING
function, then theGROUPING
function returns a value of 0.
In the latter case, the column can be null as well as non-null. In any case, it's for a particular value of that column, not a subtotal across multiple values.
Examples
The following query shows how the return values of the GROUPING
function correspond to the subtotal lines:
In the three right-most columns displaying the output of the GROUPING
functions, a value of 1 appears on a subtotal line wherever a subtotal is taken across values of the corresponding columns:
You can use these indicators as screening criteria for particular subtotals. For example, using the previous query, you can display only those subtotals for loc
and dname
combinations by using the GROUPING
function in a HAVING
clause:
This query produces the following result:
You can use the GROUPING
function to distinguish a subtotal row from a base aggregate row or from certain subtotal rows. These subtotal rows have one item in the expression list that returns null as a result of the column on which the expression is based being null for one or more rows in the table. This is opposed to representing a subtotal over the column.
To illustrate this point, the following row is added to the emp
table, which provides a row with a null value for the job
column:
The following query is issued using a reduced number of rows for clarity:
The output contains two rows containing BOSTON
in the loc
column and spaces in the job
column (fourth and fifth entries in the table):
The fifth row, where the GROUPING
function on the job
column (gf_job)
returns 1, indicates this is a subtotal over all jobs. The row contains a subtotal value of 9 in the employees
column.
In the fourth row, the GROUPING
function on the job
column as well as on the loc
column returns 0. This indicates that this is a base aggregate of all rows, where loc
is BOSTON
and job
is null, which is the row inserted for this example. The employees
column contains 1, which is the count of the single such row inserted.
In the ninth row (next to last) the GROUPING
function on the job
column returns 0, while the GROUPING
function on the loc
column returns 1. This indicates that this is a subtotal over all locations where the job
column is null which, again, is a count of the single row inserted for this example.