| --- |
| { |
| "title": "ARRAY_AGG", |
| "language": "en", |
| "description": "Concatenates the values (including null values) in a column into an array, which can be used for pivoting rows into columns." |
| } |
| --- |
| |
| ## Description |
| |
| Concatenates the values (including null values) in a column into an array, which can be used for pivoting rows into columns. |
| |
| ## Syntax |
| |
| ```sql |
| ARRAY_AGG(<col>) |
| ``` |
| |
| ## Parameters |
| |
| | Parameter | Description | |
| | -- | -- | |
| | `<col>` | An expression that determines the values to be placed into the array. Supported types: Bool, TinyInt, SmallInt, Integer, BigInt, LargeInt, Float, Double, Decimal, Date, Datetime, IPV4, IPV6, String, Array, Map, Struct. | |
| |
| ## Return Value |
| |
| Returns a value of ARRAY type.Special cases: |
| |
| - The order of elements in the array is not guaranteed. |
| - Returns the array generated by the conversion. The element type in the array is consistent with the type of col. |
| |
| |
| ## Example |
| |
| ```sql |
| -- setup |
| CREATE TABLE test_doris_array_agg ( |
| c1 INT, |
| c2 INT |
| ) DISTRIBUTED BY HASH(c1) BUCKETS 1 |
| PROPERTIES ("replication_num" = "1"); |
| INSERT INTO test_doris_array_agg VALUES (1, 10), (1, 20), (1, 30), (2, 100), (2, 200), (3, NULL); |
| ``` |
| |
| ```sql |
| select c1, array_agg(c2) from test_doris_array_agg group by c1; |
| ``` |
| |
| ```text |
| +------+---------------+ |
| | c1 | array_agg(c2) | |
| +------+---------------+ |
| | 1 | [10, 20, 30] | |
| | 2 | [100, 200] | |
| | 3 | [null] | |
| +------+---------------+ |
| ``` |
| |
| ```sql |
| select array_agg(c2) from test_doris_array_agg where c1 is null; |
| ``` |
| |
| ```text |
| +---------------+ |
| | array_agg(c2) | |
| +---------------+ |
| | [] | |
| +---------------+ |
| ``` |
| | 1 | ["a","b"] | |
| |
| |