ARRAY_AGG

functionsupportedCore SQL functions

docs.snowflake.com ↗

Aggregate functionsWindow functionsSemi-structured and structured data functions

Returns the input values, pivoted into an array. If the input is empty, the function returns an empty array.

Syntax

supportedpartialnot supportednot scored

Aggregate function

ARRAY_AGG( [ DISTINCT ] <expr1> ) [ WITHIN GROUP ( <orderby_clause> ) ]

Window function

ARRAY_AGG( [ DISTINCT ] <expr1> )
  [ WITHIN GROUP ( <orderby_clause> ) ]
  OVER ( [ PARTITION BY <expr2> ] [ ORDER BY <expr3> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] ] [ <window_frame> ] )

Parameters

NameRequiredDescription
expr1requiredAn expression (typically a column name) that determines the values to be put into the array.
OVER()requiredThe OVER clause specifies that the function is being used as a window function. For details, see Window function syntax and usage.
DISTINCToptionalRemoves duplicate values from the array.
WITHIN GROUP orderby_clauseoptionalClause that contains one or more expressions (typically column names) that determine the order of the values in each array. The WITHIN GROUP(ORDER BY) syntax supports the same parameters as the main ORDER BY clause in a SELECT statement. See ORDER BY.
PARTITION BY expr2optionalWindow function clause that specifies an expression (typically a column name). This expression defines partitions that group the input rows before the function is applied. For details, see Window function syntax and usage.
ORDER BY expr3 [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ {window_frame} ]optionalOptional expression to order by within each partition, followed by an optional window frame. For detailed window_frame syntax, see Window function syntax and usage. When this function is used with a range-based frame, the ORDER BY clause supports only a single column. Row-based frames do not have this restriction.

Returns

Returns a value of type ARRAY. The maximum amount of data that ARRAY_AGG can return for a single call is 128 MB.

Usage notes