One way is to use GROUP_CONCAT
SELECT t1.c1,
group_concat(t2.c2)
FROM
(SELECT 'a' AS c1,
1 AS k) t1
JOIN
(SELECT *
FROM
(SELECT 'b' AS c2,
1 AS k),
(SELECT 'c' AS c2,
1 AS k),
(SELECT 'd' AS c2,
1 AS k),
(SELECT 'e' AS c2,
1 AS k),
(SELECT 'f' AS c2,
1 AS k)) t2 ON t1.k=t2.k
GROUP BY t1.c1
this produces:
+-----+-------+-----------+---+
| Row | t1_c1 | f0_ | |
+-----+-------+-----------+---+
| 1 | a | b,c,d,e,f | |
+-----+-------+-----------+---+
the other is a using NEST (*disclaimer applies read further)
SELECT t1.c1,
nest(t2.c2)
FROM
(SELECT 'a' AS c1,
1 AS k) t1
JOIN
(SELECT *
FROM
(SELECT 'b' AS c2,
1 AS k),
(SELECT 'c' AS c2,
1 AS k),
(SELECT 'd' AS c2,
1 AS k),
(SELECT 'e' AS c2,
1 AS k),
(SELECT 'f' AS c2,
1 AS k)) t2 ON t1.k=t2.k
GROUP BY t1.c1
But this must be written to a table as, BigQuery automatically flattens query results, so if you use the NEST function on the top level query, the results won't contain repeated fields. Use the NEST function when using a subselect that produces intermediate results for immediate use by the same query.
On the interface by default, BigQuery flattens all query results. To preserve nested and repeated results, select a destination table and enable Allow Large Results, then uncheck the Flatten results
option.
*But there is a known bug with this feature when you are using to write to a destination table: Save a result set containing repeated field to a destination table
- If you are not saving the result to a destination table it might be just good.