ARRAY_AGG
functionsupportedCore SQL functionsReturns 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
| Name | Required | Description |
|---|---|---|
expr1 | required | An expression (typically a column name) that determines the values to be put into the array. |
OVER() | required | The OVER clause specifies that the function is being used as a window function. For details, see Window function syntax and usage. |
DISTINCT | optional | Removes duplicate values from the array. |
WITHIN GROUP orderby_clause | optional | Clause 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 expr2 | optional | Window 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} ] | optional | Optional 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
- If you do not specify WITHIN GROUP(ORDER BY), the order of elements within each array is unpredictable. (An ORDER BY clause outside the WITHIN GROUP clause applies to the order of the output rows, not to the order of the array elements within a row.)
- If you specify a number for an expression in WITHIN GROUP(ORDER BY), this number is parsed as a numeric constant, not as the ordinal position of a column in the SELECT list. Therefore, do not specify numbers as WITHIN GROUP(ORDER BY) expressions.
- If you specify DISTINCT and WITHIN GROUP, both must refer to the same column. For example: Copy code SELECT ARRAY_AGG(DISTINCT O_ORDERKEY) WITHIN GROUP (ORDER BY O_ORDERKEY) ...; If you specify different columns for DISTINCT and WITHIN GROUP, an error occurs: Copy code SELECT ARRAY_AGG(DISTINCT O_ORDERKEY) WITHIN GROUP (ORDER BY O_ORDERSTATUS) ...; SQL compilation error: [ORDERS.O_ORDERSTATUS] is not a valid order by expression You must either specify the same column for DISTINCT and WITHIN GROUP or omit DISTINCT.
- DISTINCT and WITHIN GROUP are supported for window function calls only when there is no ORDER BY clause within the OVER clause. When an ORDER BY clause is used in the OVER clause, values in the output array follow the same default order (that is, the order equivalent to WITHIN GROUP (ORDER BY expr3)).
- NULL values are omitted from the output.