ARRAY_AGG (Lakehouse v1)
The ARRAY_AGG function (also known by its alias LIST) transforms all the values, including NULL, of a specific column in a query result into an array.
Analyze Syntax
Section titled “Analyze Syntax”func.array_agg(<expr>)Analyze Examples
Section titled “Analyze Examples”table.movie_title, func.array_agg(table.rating).alias('ratings')
| movie_title | ratings ||-------------|------------|| Inception | [5, 4, 5] |SQL Syntax
Section titled “SQL Syntax”ARRAY_AGG(<expr>)
LIST(<expr>)Arguments
Section titled “Arguments”| Arguments | Description |
|---|---|
<expr> | Any expression |
Return Type
Section titled “Return Type”Returns an Array with elements that are of the same type as the original data.
SQL Examples
Section titled “SQL Examples”This example demonstrates how the ARRAY_AGG function can be used to aggregate and present data in a convenient array format:
-- Create a table and insert sample dataCREATE TABLE movie_ratings ( id INT, movie_title VARCHAR, user_id INT, rating INT);
INSERT INTO movie_ratings (id, movie_title, user_id, rating)VALUES (1, 'Inception', 1, 5), (2, 'Inception', 2, 4), (3, 'Inception', 3, 5), (4, 'Interstellar', 1, 4), (5, 'Interstellar', 2, 3);
-- List all ratings for Inception in an arraySELECT movie_title, ARRAY_AGG(rating) AS ratingsFROM movie_ratingsWHERE movie_title = 'Inception'GROUP BY movie_title;
| movie_title | ratings ||-------------|------------|| Inception | [5, 4, 5] |