ROW_NUMBER
ROW_NUMBER()
Number of the current row within its partition, counting from 1.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT ROW_NUMBER() OVER()
FROM examples
RANK
RANK()
Rank of the current row with gaps; same as row_number of its first peer.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, RANK() OVER(ORDER BY name)
FROM examples
Note on order of results with
ORDER BY
ORDER BY
inside of a window function may not affect the row ordering in the output columns. The only thingORDER BY
guarantees is the order in which the window function is applied across a partition. See our documentation on theOver
function here for more information.
DENSE_RANK
DENSE_RANK()
Rank of the current row without gaps; this function counts peer groups.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, DENSE_RANK() OVER(ORDER BY name)
FROM examples
PERCENT_RANK
PERCENT_RANK()
Relative rank of the current row: (rank - 1) / (total rows - 1).
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, PERCENT_RANK() OVER(ORDER BY name)
FROM examples
CUME_DIST
CUME_DIST()
Relative rank of the current row: (number of rows preceding or peer with current row) / (total rows).
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, CUME_DIST() OVER(ORDER BY name)
FROM examples
NTILE
NTILE(num_buckets)
Integer ranging from 1 to the argument value, dividing the partition as equally as possible.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, NTILE(2) OVER(ORDER BY name)
FROM examples
LAG
LAG(value [, offset [, default ]])
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. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, LAG(name, 2) OVER(ORDER BY name)
FROM examples
LEAD
LEAD(value [, offset [, default ]])
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. Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, LEAD(name, 2) OVER(ORDER BY name)
FROM examples
FIRST_VALUE
FIRST_VALUE(value)
Returns value evaluated at the row that is the first row of the window frame.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, FIRST_VALUE(name) OVER(ORDER BY name ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM examples
LAST_VALUE
LAST_VALUE(value)
Returns value evaluated at the row that is the last row of the window frame.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, LAST_VALUE(name) OVER(ORDER BY name ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM examples
NTH_VALUE
NTH_VALUE(value, nth)
Returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row.
WITH examples AS (
SELECT 'John' AS name
UNION ALL SELECT 'Jane'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'Angela'
UNION ALL SELECT 'James'
)
SELECT name, NTH_VALUE(name, 2) OVER(ORDER BY name ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM examples
All of the functions listed above depend on the sort ordering specified by the ORDER BY
clause of the associated window definition. Rows that are not distinct in the ORDER BY ordering are peers; the four ranking functions are defined so that they give the same answer for any two peers.
Note that FIRST_VALUE
, LAST_VALUE
, and NTH_VALUE
consider only the rows within the window frame.