OVER clause, which can partition rows into frames. Different
types of functions can be used to aggregate data
over a sliding window.
Window Function Syntax
Parameters
<window function>
<window function>
One of the supported window functions listed below
PARTITION BY
PARTITION BY
Optional list of columns and/or column expressions to use in partitioning the data. The window
function will be applied separately to each partition, defined by the set of records containing
the same value(s) for the specified partition-by column(s). If no
PARTITION BY clause is
given, the window function will be calculated over the entire data set.ORDER BY
ORDER BY
Clause defining the ordering of records within each partition before applying the window
function; optional when using The default sort order is ascending (
FIRST_VALUE() or LAST_VALUE().The ordering expression syntax is:ASC). The default null ordering is NULLS FIRST when
using ascending order and NULLS LAST when using descending order.Only one column can be specified in the ordering expression list when
using
RANGE. When using ROWS, the frame is applied after any
ordering; so, while several columns may appear in the order expression list,
there will be only one ROWS clause following the list.RANGE
RANGE
Range-based frame used in applying the window function to each record. The frame is based on the
values in the column or column expression used in the For a frame between a specified lower and upper bound:The frame bound can be one of the following:
ORDER BY clause. All records with this
value within the specified relation to the current record’s value will be used in calculating its
window function result.A range-based frame can be specified for any aggregate function
or the FIRST_VALUE() or LAST_VALUE() ranking function.For a frame between a specified lower bound and the current row:| Frame Bound | Description |
|---|---|
UNBOUNDED PRECEDING | All records in the partition before the one at the upper bound; cannot be used as an upper bound |
<number> PRECEDING | All records in the partition with an ORDER BY value between the current record’s value and <number> less than the current record’s value, inclusive |
CURRENT ROW | The current record being processed by the window function, as well as all peer rows (rows with the same ordering value) |
<number> FOLLOWING | All records in the partition with an ORDER BY value between the current record’s value and <number> more than the current record’s value, inclusive |
UNBOUNDED FOLLOWING | All records in the partition after the one at the lower bound; cannot be used as a lower bound |
ROWS
ROWS
Row-based frame used in applying the window function to each record. The frame is based on the
position of records, as determined by the For a frame between a specified lower and upper bound:The frame bound can be one of the following:
ORDER BY clause. All records within the specified
ordered distance from a given record will be used in calculating its window function value.A row-based frame can be specified for any aggregate function
or the FIRST_VALUE() or LAST_VALUE() ranking function.For a frame between a specified lower bound and the current row:| Frame Bound | Description |
|---|---|
UNBOUNDED PRECEDING | All records in the partition before the one at the upper bound; cannot be used as an upper bound |
<number> PRECEDING | All records in the partition from the one <number> records before the current record through the record at the upper bound |
CURRENT ROW | The current record being processed by the window function |
<number> FOLLOWING | All records in the partition from the record at the lower bound through the one <number> records after the current record |
UNBOUNDED FOLLOWING | All records in the partition after the one at the lower bound; cannot be used as a lower bound |
<alias>
<alias>
Column alias to apply to the window function result column
Aggregate Functions
AVG(expr)
AVG(expr)
Calculates the average of the given expression
expr over the specified window frameCOUNT(expr)
COUNT(expr)
Calculates the count of the given expression
expr over the specified window frameMAX(expr)
MAX(expr)
Calculates the maximum value of the given expression
expr over the specified window frameMEAN(expr)
MEAN(expr)
Alias for
AVG(). Calculates the average of the given expression expr over the specified
window frameMIN(expr)
MIN(expr)
Calculates the minimum value of the given expression
expr over the specified window framePRODUCT(expr)
PRODUCT(expr)
Calculates the product of the given expression
expr over the specified window frameRATIO_TO_REPORT(expr)
RATIO_TO_REPORT(expr)
Calculates the ratio of the value of
expr to the sum of expr over the specified window
frame. Note that ORDER BY is not supported for this function.STDDEV(expr)
STDDEV(expr)
Alias for
STDDEV_POP(). Calculates the population standard deviation of the given
expression expr over the specified window frameSTDDEV_POP(expr)
STDDEV_POP(expr)
Calculates the population standard deviation of the given expression
expr over the specified
window frameSTDDEV_SAMP(expr)
STDDEV_SAMP(expr)
Calculates the sample standard deviation of the given expression
expr over the specified
window frameSUM(expr)
SUM(expr)
Calculates the sum of the given expression
expr over the specified window frameVAR(expr)
VAR(expr)
Alias for
VAR_POP(). Calculates the population variance of the given expression expr over
the specified window frameVAR_POP(expr)
VAR_POP(expr)
Calculates the population variance of the given expression
expr over the specified window
frameVAR_SAMP(expr)
VAR_SAMP(expr)
Calculates the sample variance of the given expression
expr over the specified window frameRanking Functions
CUME_DIST()
CUME_DIST()
The relative position of the current row within the cumulative distribution of the selected
partition, expressed as a percentage from This function is shorthand for using the
0 (exclusive) to 1 (inclusive). The formula for
this calculation is as follows:COUNT() function in separate partition statements to
arrive at the same result:DENSE_RANK()
DENSE_RANK()
Number of the current row within the selected partition except rows with identical values
evaluate to different ranks. Starts at 1
FIRST_VALUE(<column>) [<IGNORE | RESPECT> NULLS]
FIRST_VALUE(<column>) [<IGNORE | RESPECT> NULLS]
The value found in the first row within a frame of the given expression. Optionally, add
IGNORE NULLS or RESPECT NULLS to the function syntax to ignore or respect nulls,
respectively.LAG(<column>[, <num>]) [<IGNORE | RESPECT> NULLS]
LAG(<column>[, <num>]) [<IGNORE | RESPECT> NULLS]
The value of the row before the given expression’s value. Provide an additional comma-separated
value to specify which row to select, e.g.,
LAG(vendor_id, 3) would list the value in the
vendor_id column from three rows prior to the current row. Optionally, add IGNORE NULLS
or RESPECT NULLS to the function syntax to ignore or respect nulls respectively.LAST_VALUE(<column>) [<IGNORE | RESPECT> NULLS]
LAST_VALUE(<column>) [<IGNORE | RESPECT> NULLS]
The value found in the last row within a frame of the given expression. Optionally, add
IGNORE NULLS or RESPECT NULLS to the function syntax to ignore or respect
nulls respectively.LEAD(<column>[, <num>]) [<IGNORE | RESPECT> NULLS]
LEAD(<column>[, <num>]) [<IGNORE | RESPECT> NULLS]
The value of the row after the given expression’s value. Provide an additional comma-separated
value to specify which row to select, e.g.,
LEAD(vendor_id, 3) would list the value in the
vendor_id column from three rows after the current row. Optionally, add IGNORE NULLS
or RESPECT NULLS to the function syntax to ignore or respect nulls respectively.NTILE(<num of groups>)
NTILE(<num of groups>)
The group number of the row after partitioning the rows into
num of groups groups. For
example, NTILE(4) will partition data by quartiles and return the associated group number,
1 to 4.PERCENT_RANK()
PERCENT_RANK()
The rank of the current row within the selected partition, expressed as a percentage from This function is shorthand for using the
0
to 1, inclusive. The formula for this calculation is as follows:RANK() & COUNT() functions in separate partition
statements to arrive at the same result:RANK()
RANK()
Number of the current row within the selected partition. However, rows with identical values
evaluate to the same rank. Starts at 1
ROW_NUMBER()
ROW_NUMBER()
Number of the current row within the selected partition. Starts at 1
Examples
To calculate the rolling sum of total amounts collected by each taxi vendor over the course of a given day, as well as the number of other trips that occurred within 5 minutes of each trip:Window Rolling Sum Example
Window Moving Average Example
Window Ranking Example
Window FIRST_VALUE Example
Window N-Tile Example