ARRAY_AGG (Lakehouse v1)
La fonction ARRAY_AGG (également connue sous son alias LIST) transforme toutes les valeurs d’une colonne donnée d’un résultat de requête, y compris NULL, en un tableau.
Syntaxe Analyze
Section intitulée « Syntaxe Analyze »func.array_agg(<expr>)Exemples Analyze
Section intitulée « Exemples Analyze »table.movie_title, func.array_agg(table.rating).alias('ratings')
| movie_title | ratings ||-------------|------------|| Inception | [5, 4, 5] |Syntaxe SQL
Section intitulée « Syntaxe SQL »ARRAY_AGG(<expr>)
LIST(<expr>)Arguments
Section intitulée « Arguments »| Arguments | Description |
|---|---|
<expr> |
Toute expression |
Type de retour
Section intitulée « Type de retour »Renvoie un tableau dont les éléments sont du même type que les données d’origine.
Exemples SQL
Section intitulée « Exemples SQL »Cet exemple montre comment la fonction ARRAY_AGG permet d’agréger et de présenter des données sous la forme pratique d’un tableau :
-- 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] |