max_by aggregate function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns the value of an expr1 associated with the maximum value of expr2 in a group.

Syntax

max_by(expr1, expr2) [FILTER ( WHERE cond ) ]

This function can also be invoked as a window function using the OVER clause.

Arguments

  • expr1: An expression of any type.

  • expr2: An expression of a type that can be ordered.

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

Returns

The result type matches the type of expr1.

If STRING.

This function is non-deterministic if expr2 is not unique within the group.

Note

For certain STRING collations, such as UTF8_LCASE, the result may be non-deterministic as well.

Examples

> SELECT max_by(x, y) FROM VALUES (('a', 10)), (('b', 50)), (('c', 20)) AS tab(x, y);
 b

> SELECT max_by(x, y COLLATE UTF8_LCASE) FROM VALUES (('a', 'X')), (('b', 'x')), (('c', 'v')) AS tab(x, y);
 a (or b)