BigQuery: Aggregate multiple fields into array

ajor picture ajor · Jul 21, 2017 · Viewed 11k times · Source

I have some data where for each ID I want to aggregate two or more fields into an array, and I want them to match in terms of order.

So for example if I have the following data:

enter image description here

I want to turn it into this:

enter image description here

Alternatively, something like this would also be fine:

enter image description here

So firstly, if I were to use a query like this, would it do what I want or does it not guarantee that the two fields pull through in the same order (i.e. that the corresponding values in Value_1 and Value_2 may not match)?

SELECT
  ID,
  ARRAY_AGG (
    Value_1
  ) AS Value_1,
  ARRAY_AGG (
    Value_2
  ) AS Value_2

FROM
  table

GROUP BY
  ID

If not, how can I go about doing this?

Answer

Elliott Brossard picture Elliott Brossard · Jul 21, 2017

Use ARRAY_AGG with STRUCT if you want to pair the values together. For example,

SELECT
  ID,
  ARRAY_AGG (
    STRUCT(Value_1, Value_2)
  ) AS Values
FROM
  table
GROUP BY
  ID;