General-purpose window functions
row_number()
The row_number()
function assigns a unique sequential integer to each row within a partition of a result set. The numbering starts at 1 for the first row in each partition and increments by 1 for each subsequent row.
row_number()
can be used to turn non-unique rows into unique rows. This could be used to eliminate duplicate rows.
Syntax
Example
We recommend using
row_number()
only for top-N pattern queries. For details about this pattern, see Top-N by group.rank()
rank()
returns the rank of the current row, with gaps; that is, the row_number
of the first row in its peer group.
Syntax
Example
dense_rank()
dense_rank()
returns the rank of the current row, without gaps; that is, if some rows share the same rank, the row next to them is assigned the next consecutive rank.
Syntax
Example
lag()
and lead()
lag()
allows you to access the value of a previous row in the result set. You can specify the number of rows to look back.
Syntax
Example
lead()
is similar to lag()
, but it allows you to access the value of a subsequent row in the result set.
Syntax
Example
first_value()
and last_value()
Added in v2.3.0: Support
IGNORE NULLS
.first_value()
function returns the value of the first row in the current window frame. If IGNORE NULLS
is present, first_value()
returns the first non-null value.
Syntax
Example
last_value()
returns the value of the last row in the current window frame. If IGNORE NULLS
is present, last_value()
returns the last non-null value.
Syntax
Example
Example
Named window
Added in v2.5.0.
OVER
clause can be error-prone. To avoid redundancy, you can define a named window in the WINDOW
clause of a query and reference it by name in window function calls.
Syntax
Example
WINDOW
keyword is now a reserved keyword and cannot be directly used as table alias.
Aggregate window functions
All aggregate functions, including builtin ones such assum()
and min()
, user-defined ones and AGGREGATE:
-prefixed scalar functions, can be used as window functions.
For the complete list of builtin aggregate functions and their usage, see Aggregate functions.