- Categories:
Aggregate functions (Semi-structured Data) , Window functions (General) , Semi-structured and structured data functions (Array/Object)
ARRAY_AGG¶
Returns the input values, pivoted into an array. If the input is empty, the function returns an empty array.
- Aliases:
ARRAYAGG
Syntax¶
Aggregate function
Window function
Arguments¶
Required:
expr1An expression (typically a column name) that determines the values to be put into the array.
OVER()The OVER clause specifies that the function is being used as a window function. For details, see Window function syntax and usage.
Optional:
DISTINCTRemoves duplicate values from the array.
WITHIN GROUP orderby_clauseClause 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 expr2Window 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 expression to order by within each partition, followed by an optional window frame. For detailed
window_framesyntax, 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.
LIMIT is not supported.
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:
If you specify different columns for DISTINCT and WITHIN GROUP, an error occurs:
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.
Examples¶
The example queries below use the tables and data shown below:
This example shows non-pivoted output from a query that does not use ARRAY_AGG(). The contrast in output between this example and the following example shows that ARRAY_AGG() pivots the data.
This example shows how to use ARRAY_AGG() to pivot a column of output into an array in a single row:
This example shows the use of the DISTINCT keyword with ARRAY_AGG().
This example uses two separate ORDER BY clauses. One controls the order within the output array inside each row, and the other controls the order of the output rows:
The following example uses a different data set. The ARRAY_AGG function is called as a window function with a ROWS BETWEEN window frame. First, create the table and load it with 14 rows:
Now run the following query. Note that only a partial result set is shown here.