Skip to main content
Window functions are available through the use of the 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
The default frame type is:

Parameters

<window function>

One of the supported window functions listed below

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

Clause defining the ordering of records within each partition before applying the window function; optional when using FIRST_VALUE() or LAST_VALUE().The ordering expression syntax is:
The default sort order is ascending (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-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 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:
For a frame between a specified lower and upper bound:
The frame bound can be one of the following:
Frame BoundDescription
UNBOUNDED PRECEDINGAll records in the partition before the one at the upper bound; cannot be used as an upper bound
<number> PRECEDINGAll 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 ROWThe current record being processed by the window function, as well as all peer rows (rows with the same ordering value)
<number> FOLLOWINGAll 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 FOLLOWINGAll records in the partition after the one at the lower bound; cannot be used as a lower bound

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 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:
For a frame between a specified lower and upper bound:
The frame bound can be one of the following:
Frame BoundDescription
UNBOUNDED PRECEDINGAll records in the partition before the one at the upper bound; cannot be used as an upper bound
<number> PRECEDINGAll records in the partition from the one <number> records before the current record through the record at the upper bound
CURRENT ROWThe current record being processed by the window function
<number> FOLLOWINGAll records in the partition from the record at the lower bound through the one <number> records after the current record
UNBOUNDED FOLLOWINGAll records in the partition after the one at the lower bound; cannot be used as a lower bound

<alias>

Column alias to apply to the window function result column

Aggregate Functions

AVG(expr)

Calculates the average of the given expression expr over the specified window frame

COUNT(expr)

Calculates the count of the given expression expr over the specified window frame

MAX(expr)

Calculates the maximum value of the given expression expr over the specified window frame

MEAN(expr)

Alias for AVG(). Calculates the average of the given expression expr over the specified window frame

MIN(expr)

Calculates the minimum value of the given expression expr over the specified window frame

PRODUCT(expr)

Calculates the product of the given expression expr over the specified window frame

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)

Alias for STDDEV_POP(). Calculates the population standard deviation of the given expression expr over the specified window frame

STDDEV_POP(expr)

Calculates the population standard deviation of the given expression expr over the specified window frame

STDDEV_SAMP(expr)

Calculates the sample standard deviation of the given expression expr over the specified window frame

SUM(expr)

Calculates the sum of the given expression expr over the specified window frame

VAR(expr)

Alias for VAR_POP(). Calculates the population variance of the given expression expr over the specified window frame

VAR_POP(expr)

Calculates the population variance of the given expression expr over the specified window frame

VAR_SAMP(expr)

Calculates the sample variance of the given expression expr over the specified window frame

Ranking Functions

CUME_DIST()

The relative position of the current row within the cumulative distribution of the selected partition, expressed as a percentage from 0 (exclusive) to 1 (inclusive). The formula for this calculation is as follows:
This function is shorthand for using the COUNT() function in separate partition statements to arrive at the same result:

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]

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]

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]

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]

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>)

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()

The rank of the current row within the selected partition, expressed as a percentage from 0 to 1, inclusive. The formula for this calculation is as follows:
This function is shorthand for using the RANK() & COUNT() functions in separate partition statements to arrive at the same result:

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()

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
To calculate a 5-before and 10-after moving average of 4-passenger trip distances per vendor over the course of a given day:
Window Moving Average Example
To rank, by vendor, the total amounts collected from 3-passenger trips on a given day:
Window Ranking Example
To compare each trip’s total amount to the lowest (ignoring nulls), highest (ignoring nulls), & average total amount for 5-passenger trips for each vendor over the course of a given day:
Window FIRST_VALUE Example
To compare each vendor’s average total amount to their average total amount within the interquartile range:
Window N-Tile Example