may specify zero or more columns
or expressions and is interpreted the same way as though it were directly
in the <literal>GROUP BY</literal> clause. An empty grouping set means that all
rows are aggregated down to a single group (which is output even if no
input rows were present), as described above for the case of aggregate
functions with no <literal>GROUP BY</literal> clause.
</para>
<para>
References to the grouping columns or expressions are replaced
by null values in result rows for grouping sets in which those
columns do not appear. To distinguish which grouping a particular output
row resulted from, see <xref linkend="functions-grouping-table"/>.
</para>
<para>
A shorthand notation is provided for specifying two common types of grouping set.
A clause of the form
<programlisting>
ROLLUP ( <replaceable>e1</replaceable>, <replaceable>e2</replaceable>, <replaceable>e3</replaceable>, ... )
</programlisting>
represents the given list of expressions and all prefixes of the list including
the empty list; thus it is equivalent to
<programlisting>
GROUPING SETS (
( <replaceable>e1</replaceable>, <replaceable>e2</replaceable>, <replaceable>e3</replaceable>, ... ),
...
( <replaceable>e1</replaceable>, <replaceable>e2</replaceable> ),
( <replaceable>e1</replaceable> ),
( )
)
</programlisting>
This is commonly used for analysis over hierarchical data; e.g., total
salary by department, division, and company-wide total.
</para>
<para>
A clause of the form
<programlisting>
CUBE ( <replaceable>e1</replaceable>, <replaceable>e2</replaceable>, ... )
</programlisting>
represents the given list and all of its possible subsets (i.e., the power
set). Thus
<programlisting>
CUBE ( a, b, c )
</programlisting>
is equivalent to
<programlisting>
GROUPING SETS (
( a, b, c ),
( a, b ),
( a, c ),
( a ),
( b, c ),
( b ),
( c ),
( )
)
</programlisting>
</para>
<para>
The individual elements of a <literal>CUBE</literal> or <literal>ROLLUP</literal>
clause may be either individual expressions, or sublists of elements in
parentheses. In the latter case, the sublists are treated as single
units for the purposes of generating the individual grouping sets.
For example:
<programlisting>
CUBE ( (a, b), (c, d) )
</programlisting>
is equivalent to
<programlisting>
GROUPING SETS (
( a, b, c, d ),
( a, b ),
( c, d ),
( )
)
</programlisting>
and
<programlisting>
ROLLUP ( a, (b, c), d )
</programlisting>
is equivalent to
<programlisting>
GROUPING SETS (
( a, b, c, d ),
( a, b, c ),
( a ),
( )
)
</programlisting>
</para>
<para>
The <literal>CUBE</literal> and <literal>ROLLUP</literal> constructs can be used either
directly in the <literal>GROUP BY</literal> clause, or nested inside a
<literal>GROUPING SETS</literal> clause. If one <literal>GROUPING SETS</literal> clause
is nested inside another, the effect is the same as if all the elements of
the inner clause had been written directly in the outer clause.
</para>
<para>
If multiple grouping items are specified in a single <literal>GROUP BY</literal>
clause, then the final list of grouping sets is the Cartesian product of the
individual items. For example:
<programlisting>
GROUP BY a, CUBE (b, c), GROUPING SETS ((d), (e))
</programlisting>
is equivalent to
<programlisting>
GROUP BY GROUPING SETS (
(a, b, c, d), (a, b, c, e),
(a, b, d), (a, b, e),
(a, c, d), (a, c, e),
(a, d), (a, e)
)
</programlisting>
</para>
<para>
<indexterm zone="queries-grouping-sets">
<primary>ALL</primary>
<secondary>GROUP BY ALL</secondary>
</indexterm>
<indexterm zone="queries-grouping-sets">
<primary>DISTINCT</primary>
<secondary>GROUP