12.5. Groupings, Rollups, and Cubes

OLAP functions add the GROUPING, ROLLUP, and CUBE extensions to the GROUP BY clause. They can be written in older Standard SQL using GROUP BY and UNION operators, so they are really shorthand and not brand-new functionality.

They return a single result set that has mixed levels of aggregation. In some ways, the mixed levels are a violation of “relational purity,” but they are handy for basic hierarchical reporting. You will see those clauses called “super groups” in the literature.

12.5.1. Grouping Set Clause

The first member of the family is the GROUPING SET. It is really just shorthand for a UNION of several similar grouped queries. It might be easier to see if we build up the options in this subclause. ...

Get Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.