Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery creat repeated record field from query

Is it possible to create a repeated record column in BigQuery? For example, for the following data:

| a | b | c |
-------------
| 1 | 5 | 2 |
-------------
| 1 | 3 | 1 |
-------------
| 2 | 2 | 1 |

Is the following possible?

Select a, NEST(b, c) as d from *table* group by a

To produce the following results

| a | d.b | d.c |
-----------------
| 1 |  5  |  2  |
-----------------
|   |  3  |  1  |
-----------------
| 2 |  2  |  1  |
like image 495
Fardream Avatar asked Jan 11 '16 21:01

Fardream


1 Answers

With introduction of BigQuery Standard SQL we've got easy way to deal with records
Try below, Don't forget to uncheck Use Legacy SQL checkbox under Show Options

WITH YourTable AS (
  SELECT 1 AS a, 5 AS b, 2 AS c UNION ALL
  SELECT 1 AS a, 3 AS b, 1 AS c UNION ALL
  SELECT 2 AS a, 2 AS b, 1 AS c
)
SELECT a, ARRAY_AGG(STRUCT(b, c)) AS d
FROM YourTable 
GROUP BY a
like image 181
Mikhail Berlyant Avatar answered Sep 25 '22 00:09

Mikhail Berlyant