15.2. OLAP Functions

The introduction of OLAP functions in Standard SQL made it possible to do all kinds of row numberings easily. I would like to stress that these are functions, and they behave like other SQL functions in spite of their strange syntax.

15.2.1. Simple Row Numbering

The ROW_NUMBER() OVER() is pretty simple to understand. The window clause works the same way. PARTITION BY creates partitions, just as it did with the aggregate functions. The ORDER BY clause sorts the rows within the partition and assigns a number from 1 to (n); if no ORDER BY clause is given, then the results are unpredictable. Since the ORDER BY applies to the whole partition, a RANGE clause makes no sense.

It does not make much sense to use a ROW_NUMBER() without ...

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.