REDUCE

functionsupportedCore SQL functions

docs.snowflake.com ↗

Semi-structured and structured data functions

Reduces an array to a single value based on the logic in a lambda expression. The REDUCE function takes an array, an initial accumulator value, and a lambda function. It applies the lambda function to each element of the array, updating the accumulator with each result. After processing all elements, REDUCE returns the final accumulator value.

Syntax

REDUCE( <array> , <init> , <lambda_expression> )

Parameters

NameRequiredDescription
arrayrequiredThe array that contains the elements to be reduced. The array can be semi-structured or structured.
initrequiredThe initial accumulator value.
lambda_expressionrequiredA lambda expression that defines the reduce logic on each array element. The lambda expression must be specified in the following syntax:Copy code <acc> [ <datatype> ] , <value> [ <datatype> ] -> <expr> The acc argument is the accumulator, and the value argument is the current element being processed in the array.

Returns

This function can return a value of any data type. If the input array is empty, then the function returns the initial value of the accumulator. The function returns NULL in these cases: - The input array is NULL. - The initial value of the accumulator is NULL. - The lambda function returns NULL.

Usage notes

See also