Window

SQL Window Functions

Index

Row Number Functions

Rank Functions

Lead or Lag Functions

Nth Value Functions

Other Window Functions

Details

Row Number Functions

ROW_NUMBER

Return the number of the current row starting with 1. Window frame clause is not allowed for this function.

Example:

Rank Functions

RANK

Returns the rank of the current row. The rank of a row is the number of rows that precede this row plus 1. If two or more rows have the same values in ORDER BY columns, these rows get the same rank from the first row with same values. It means that gaps in ranks are possible. This function requires window order clause. Window frame clause is not allowed for this function.

See Rank aggregate for a hypothetical set function with the same name.

Example:

DENSE_RANK

Return the dense rank of the current row. The rank of a row is the number of groups of rows with the same values in ORDER BY columns that precede group with row plug 1. If two or more rows have the same values in ORDER BY columns, these rows get the same rank. Gaps in ranks are not possible. This function requires window order clause. Window frame clause is not allowed for this function.

See DENSE_RANK aggregate for a hypothetical set function with the same name.

Example:

PERCENT_RANK

Returns the relative rank of the current row. The relative rank is calculated as (RANK - 1) / (NR - 1), where RANK is a rank of the row and NR is a number of rows in window partition with this row. Note that result is always 0 if window order clause is not specified. Window frame clause is not allowed for this function.

See PERENT_RANK aggregate for a hypothetical set function with the same name.

Example:

CUME_DIST

Returns the relative rank of the current row. The relative rank is calculated as NP / NR where NP is a number of rows that precede the current row or have the same values in ORDER BY columns and NR is a number of rows in window partition with this row. Note that result is always 1 if window order clause is not specified. Window frame clause is not allowed for this function.

Example:

Lead or Lag Functions

LEAD

TODO

LAG

TODO

Nth Value Functions

FIRST_VALUE

TODO

LAST_VALUE

TODO

NTH_VALUE

TODO

Other Window Functions

NTILE

Distributes the rows into a specified number of groups. The Number of groups should be a positive long value. NTLE returns the 1-based number of the group to which the current row belongs. First groups will have more rows if the number of rows is not divisible by number of groups. For example, if 5 rows are distributed into 2 groups this function returns 1 for the first 3 rows and 2 for the last rows. This function requires window order clause. Window frame clause is not allowed for this function.

Example:

RATIO_TO_REPORT

Returns the ratio of a value to the sum of all values. If the argument is NULL or the sum of all values is 0, then the value of the function is NULL. Window ordering and window frame clauses are not allowed for this function.

Note: Window function in Guinsoo may require a lot of memory for large queries.

Example:

Last updated

Was this helpful?