ANY_VALUE

functionsupportedCore SQL functions

docs.snowflake.com ↗

Aggregate functionsWindow functions

Returns some value of the expression from the group. The result is non-deterministic.

Syntax

supportedpartialnot supportednot scored

Aggregate function

ANY_VALUE( [ DISTINCT ] <expr1> )

Window function

ANY_VALUE( [ DISTINCT ] <expr1> ) OVER ( [ PARTITION BY <expr2> ] )

Parameters

NameRequiredDescription
expr1requiredThe input expression.
expr2requiredThe column to partition on, if you want the result to be split into multiple partitions.

Returns

This function can return a value of any data type. If the input expression is NULL, the function returns NULL.

Usage notes

Using ANY_VALUE with GROUP BY statements

ANY_VALUE can simplify and optimize the performance of GROUP BY statements. A common problem for many queries is that the result of a query with a GROUP BY clause can only contain expressions used in the GROUP BY clause itself, or results of aggregate functions. For example: In this query, the customer.name attribute needs to be in the GROUP BY to be included in the result. This is unnecessary (for example, when customer.id is known to be unique) and makes the computation possibly more complex and slower. Another option is to use an aggregate function. For example: This simplifies the GROUP BY clause, but still requires computing the MIN function, which incurs an extra cost. With ANY_VALUE, you can execute the following query: