| --- | |
| { | |
| "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 (usually column names). | | |
| ## 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 varchar(20)) distributed by hash(c1) buckets 1 properties ("replication_num"="1"); | |
| insert into test_doris_array_agg values (1,'a'),(1,'b'),(2,'c'),(2,null),(3,null); | |
| ``` | |
| ```sql | |
| select * from test_doris_array_agg; | |
| ``` | |
| ```text | |
| +------+------+ | |
| | c1 | c2 | | |
| +------+------+ | |
| | 1 | a | | |
| | 1 | b | | |
| | 2 | c | | |
| | 2 | NULL | | |
| | 3 | NULL | | |
| +------+------+ | |
| ``` | |
| ```sql | |
| select c1, array_agg(c2) from test_doris_array_agg group by c1; | |
| ``` | |
| ```text | |
| +------+-----------------+ | |
| | c1 | array_agg(`c2`) | | |
| +------+-----------------+ | |
| | 1 | ["a","b"] | | |
| | 2 | [NULL,"c"] | | |
| | 3 | [NULL] | | |
| +------+-----------------+ | |
| ``` | |