ARRAY_AGG (Lakehouse v1)
A função ARRAY_AGG (também conhecida pelo alias LIST) transforma todos os valores, inclusive NULL, de uma coluna específica no resultado de uma consulta em um array.
Sintaxe do Analyze
Seção intitulada “Sintaxe do Analyze”func.array_agg(<expr>)Exemplos do Analyze
Seção intitulada “Exemplos do Analyze”table.movie_title, func.array_agg(table.rating).alias('ratings')
| movie_title | ratings ||-------------|------------|| Inception | [5, 4, 5] |Sintaxe SQL
Seção intitulada “Sintaxe SQL”ARRAY_AGG(<expr>)
LIST(<expr>)Argumentos
Seção intitulada “Argumentos”| Argumentos | Descrição |
|---|---|
<expr> |
Qualquer expressão |
Tipo de retorno
Seção intitulada “Tipo de retorno”Retorna um array com elementos do mesmo tipo que os dados originais.
Exemplos de SQL
Seção intitulada “Exemplos de SQL”Este exemplo demonstra como a função ARRAY_AGG pode ser usada para agregar e apresentar dados em um formato de array conveniente:
-- 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] |