0

There is a lot of information online to go from flattened data to arrays or structs, but I need to do the opposite and I am having a hard time archiving it. I am using Google BigQuery.

I have something like:

| Id | Value1 | Value2 |
|  1 |    1   |    2   |
|  1 |    3   |    4   |
|  2 |    5   |    6   |
|  2 |    7   |    8   |

I would like to get for the example above:

1, [(1, 2), (3, 4)]
2, [(5, 6), (7, 8)]

If I try to put an array in the select with a group by it is not a valid statement

For example:

SELECT Id, [ STRUCT(Value1, Value2) ] as Value
FROM `table.dataset`
GROUP BY Id

Which returns:

1, (1, 2)
1, (3, 4)
2, (5, 6)
2, (7, 8)

Which is not what I am looking for. The structure I got is: Id, Value.Value1, Value.Value2 and I want Id, [ Value(V1, V2), Value(V1, V2), ... ]

2 Answers 2

1

You can do that with SELECT Id, ARRAY_AGG(STRUCT(Value1, Value2)) ... GROUP BY Id

Sign up to request clarification or add additional context in comments.

Comments

1

Below is for BigQuery Standard SQL

#standardSQL
select id, array_agg((select as struct t.* except(id))) as `value`
from `project.dataset.table` t
group by id    

If to apply to sample data in your question - output is

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.