When you use
a GROUP BY in your query, the columns you specify form a grouping set. It is
possible to specify multiple sets after the GROUP BY if you add the GROUPING
SETS keywords. This needs to be followed by the sets in outer parentheses.
Inside, you separate each set with commas. Each internal grouping set of
columns itself is surrounded by parentheses.
One of the
grouping sets you specify can be the empty set, which is indicated by
parentheses with nothing inside them. This will result in a grand total of all
the rows. With the different grouping sets, each will get aggregated and
grouped separately. If there are items selected that are not part of the set,
your query results will produce a NULL for that value.
There are some special shorthand forms of
grouping sets such as CUBE and ROLLUP. They have helper functions like GROUPING
and GROUPING_ID that let you make sense of the output. I will save the details
for a future post.