hop
and slide
. Streaming windows
allow aggregating over records with a time bound, while SQL window functions
allow you to reference other rows without aggregating.
A window function call always contains an OVER clause directly following the
window function’s name and argument(s). This is what syntactically distinguishes
it from a normal function or non-window aggregate. The OVER clause determines
exactly how the rows of the query are split up for processing by the window
function. The PARTITION BY clause within OVER divides the rows into groups, or
partitions, that share the same values of the PARTITION BY expression(s). For
each row, the window function is computed across the rows that fall into the
same partition as the current row. The previous example showed how to count the
average of a column per partition.
You can also control the order in which rows are processed by window functions
using ORDER BY within OVER. (The window ORDER BY does not even have to match the
order in which the rows are output.)
Note that currently window functions must contain a streaming window in the
PARTITION BY.
Syntax
The syntax for the OVER-clause isAggregate functions
All aggregate functions can be used as window functions.Ranking functions
row_number
Number of the current row within its partition, counting from 1.
rank
Rank of the current row with gaps; same as row_number of its first peer.
dense_rank
Rank of the current row without gaps; this function counts peer groups.
ntile
Integer ranging from 1 to the argument value, dividing the partition as equally as possible.
- expression: An integer describing the number groups the partition should be split into
Analytical functions
cume_dist
Relative rank of the current row: (number of rows preceding or peer with current row) / (total rows).
percent_rank
Relative rank of the current row: (rank - 1) / (total rows - 1).
lag
Returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.
- expression: Expression to operate on
- offset: Integer. Specifies how many rows back the value of expression should be retrieved. Defaults to 1.
- default: The default value if the offset is not within the partition. Must be of the same type as expression.
lead
Returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default (which must be of the same type as value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.
- expression: Expression to operate on
- offset: Integer. Specifies how many rows forward the value of expression should be retrieved. Defaults to 1.
- default: The default value if the offset is not within the partition. Must be of the same type as expression.
first_value
Returns value evaluated at the row that is the first row of the window frame.
- expression: Expression to operate on
last_value
Returns value evaluated at the row that is the last row of the window frame.
- expression: Expression to operate on
nth_value
Returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row.
- expression: The name the column of which nth value to retrieve
- n: Integer. Specifies the n in nth