FIRST_VALUE
functionsupportedCore SQL functionsReturns the first value within an ordered group of values.
Syntax
supportedpartialnot supportednot scored
FIRST_VALUE( <expr> ) [ { IGNORE | RESPECT } NULLS ]
OVER ( [ PARTITION BY <expr1> ] ORDER BY <expr2> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ <window_frame> ] )
Parameters
| Name | Required | Description |
|---|---|---|
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 |
{ IGNORE | RESPECT } NULLS | required | Whether to ignore or respect NULL values when an expr contains NULL values: IGNORE NULLS returns the first non-NULL value. RESPECT NULLS returns a NULL value if it is the first value in the expression. Default: RESPECT NULLS |
Usage notes
- 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 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 detailed window_frame syntax, see Window function syntax and usage.