last_value aggregate function (Databricks SQL)

Returns the last value of expr for the group of rows.

Syntax

last_value(expr [, ignoreNull] ) [FILTER ( WHERE cond ) ]

Arguments

  • expr: An expression of any type.

  • ignoreNull: A optional BOOLEAN literal

  • cond: An optional boolean expression filtering the rows used for aggregation.

Returns

The result type matches expr.

If ignoreNull is true, the function returns only non-null values unless the entire group of values is NULL. The default for ignoreNull is false. The function is a synonym for last aggregate function (Databricks SQL).

This function is non-deterministic.

Examples

> SELECT last_value(col) FROM VALUES (10), (5), (20) AS tab(col);
 20
> SELECT last_value(col) FROM VALUES (10), (5), (NULL) AS tab(col);
 NULL
> SELECT last_value(col, true) FROM VALUES (10), (5), (NULL) AS tab(col);
 5