NTH_VALUE
functionsupportedCore SQL functionsReturns 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
| Name | Required | Description |
|---|---|---|
n | required | This specifies which value of N to use when looking for the Nth value. |
expr | required | The expression that determines the return value. |
expr1 | required | The 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 |
expr2 | required | The 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 } | required | Whether 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 } NULLS | required | Whether 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
- Input value n can’t be greater than 1000.
- This function is a rank-related function, so it must specify a window. A window clause consists of the following subclauses:
- PARTITION BY expr1 subclause (optional).
- ORDER BY expr2 subclause (required). For details about additional supported ordering options (sort order, ordering of NULL values, and so on), see the documentation for the ORDER BY clause, which follows the same rules.
- window_frame subclause (optional).
- The order of rows in a window (and thus the result of the query) is fully deterministic only if the keys in the ORDER BY clause make each row unique. Consider the following example: Copy code ... OVER (PARTITION BY p ORDER BY o COLLATE 'lower') ... The query result can vary if any partition contains values of column o that are identical, or would be identical in a case-insensitive comparison.
- The ORDER BY clause inside the OVER clause controls the order of rows only within the window, not the order of rows in the output of the entire query. To control output order, use a separate ORDER BY clause at the outermost level of the query.
- The optional window_frame (cumulative or sliding) specifies the subset of rows within the window for which the function is calculated. If no window_frame is specified, the default is the entire window: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING Note that this deviates from the ANSI standard, which specifies the following default for window frames: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- For more information about window frames, including syntax, usage notes, and examples, see Window function syntax and usage.