NTH_VALUE

functionsupportedCore SQL functions

docs.snowflake.com ↗

Window function syntax and usage

Returns the nth value (up to 1000) within an ordered group of values.

Syntax

supportedpartialnot supportednot scored

NTH_VALUE( <expr> , <n> ) [ FROM { FIRST | LAST } ] [ { IGNORE | RESPECT } NULLS ]
  OVER ( [ PARTITION BY <expr1> ] ORDER BY <expr2> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ <window_frame> ] )

Parameters

NameRequiredDescription
nrequiredThis specifies which value of N to use when looking for the Nth value.
exprrequiredThe expression that determines the return value.
expr1requiredThe expression by which to partition the rows. You can specify a single expression or a comma-separated list of expressions. For example:Copy code PARTITION BY column_1, column_2
expr2requiredThe expression by which to order the rows. You can specify a single expression or a comma-separated list of expressions. For example:Copy code ORDER BY column_3, column_4
FROM { FIRST | LAST }requiredWhether to ignore or respect NULL values when an expr contains NULL values: FROM FIRST starts from the beginning of the ordered list and moves forward. FROM LAST starts from the end of the ordered list and moves backward. Default: FROM FIRST
{ IGNORE | RESPECT } NULLSrequiredWhether to ignore or respect NULL values when an expr contains NULL values: IGNORE NULLS skips NULL values in the expression. RESPECT NULLS returns a NULL value if it is the nth value in the expression. Default: RESPECT NULLS

Usage notes

See also

Referenced by