PostgreSQL Window Functions
PostgreSQL window functions provide computing power across the group associated with the current row. Window functions are different from aggregate functions, which perform calculations on each group and return a row for each group, window functions do not output each group to a row and combine the calculation results of each group into each row in this group.
All window functions rely on OVER
clauses, which can be used to group all rows and sort rows within groups. The calling syntax of the window function is as follows:
window_function(args)
OVER (
[PARTITION BY partition_column_list]
[ORDER BY order_column_list]
)
In this Syntax:
- The
window_function(args)
is the window function that needs to be called. In addition to specific window functions, you can also use aggregate functions here. - The
PARTITION BY
specifies the column name that needs to be partitioned. It is optional. Defaults to one partition for the entire result set. - The
ORDER BY
specifies the sort column name. It is optional.
PostgreSQL provides many window functions that can be used to obtain information such as rank, row number, cumulative distribution, bucket rank, etc.
-
cume_dist
The PostgreSQLcume_dist()
function returns the cumulative distribution of the current row. -
dense_rank
The PostgreSQLdense_rank()
function returns the rank within the partition in which the current row is located, starting at 1, with no gap. -
first_value
The PostgreSQLfirst_value()
function returns the evaluated value from the first row of the window frame associated with the current row. -
lag
The PostgreSQLlag()
function returns the value from the specified row before the current row in the partition where the current row is located. -
last_value
The PostgreSQLlast_value()
function returns the evaluated value from the last row of the window frame associated with the current row. -
lead
The PostgreSQLlead()
function returns the value from the specified row after the current row in the partition where the current row is located. -
nth_value
The PostgreSQLnth_value()
function returns the evaluated value from the specified row of the window frame associated with the current row. -
ntile
The PostgreSQLntile()
function divides all rows in the partition into the specified number of buckets as evenly as possible, and returns the rank of the bucket where the current row is located. -
percent_rank
The PostgreSQLpercent_rank()
function returns the relative rank within the partition where the current row is located, that is(rank() - 1) / (number of rows partition - 1)
. -
rank
The PostgreSQLrank()
function returns the rank within the partition in which the current row is located, starting at 1, with gaps. -
row_number
The PostgreSQLrow_number()
function returns the row number of the current row within the partition where the current row is located, starting from 1.