Ir al contenido

La función ARRAY_AGG (también conocida por su alias LIST) transforma todos los valores, incluido NULL, de una columna específica del resultado de una consulta en una matriz.

func.array_agg(<expr>)
table.movie_title, func.array_agg(table.rating).alias('ratings')
| movie_title | ratings |
|-------------|------------|
| Inception | [5, 4, 5] |
ARRAY_AGG(<expr>)
LIST(<expr>)
Argumentos Descripción
<expr> Cualquier expresión

Devuelve una matriz con elementos del mismo tipo que los datos originales.

Este ejemplo muestra cómo se puede usar la función ARRAY_AGG para agregar y presentar los datos en un formato de matriz práctico:

-- Create a table and insert sample data
CREATE 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 array
SELECT movie_title, ARRAY_AGG(rating) AS ratings
FROM movie_ratings
WHERE movie_title = 'Inception'
GROUP BY movie_title;
| movie_title | ratings |
|-------------|------------|
| Inception | [5, 4, 5] |