33

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?

3 Answers 3

62

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;
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome...exactly what i need for a truly denormalized table
7

... and for your alternative ask:

SELECT  
  id,
  ARRAY_AGG(CONCAT('[', Value_1, ',', Value_2, ']')) AS Values
FROM `yourTable`
GROUP BY id

2 Comments

This generates a string instead of a list as concat returns a string. Is this also possible as list in list?
This is susceptible to SQL injection security vulnerability... Are we in '90s again?
-1

Working solution of Array_Agg () with Python BigQuery:

!pip install -U google-cloud-bigquery
import pandas as pd

from google.cloud import bigquery

strvalue = """SELECT users ARRAY_AGG(STRUCT(session, page )) as hasComp FROM <datasetname>.<tableName> WHERE Group by users order by users limit 100 """

bigquery_client = bigquery.Client(project="")

dataset = bigquery_client.dataset("")

table = dataset.table('')

table.view_query_legacy_sql = False

query_job = bigquery_client.query(str_value)

df = query_job.to_dataframe()

print(df)

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.