How to convert BigQuery column to array

How can I convert column to array in BqgQuery ?

BigQuery(BQ) is very useful for data analysis or processing.

It is good at handling huge data. It returns summary result in short time.

BQ has one useful data format array.

It can handle multiple data like [1,2,3] in 1 row.

But how can we create array data from column data.

So today I introduce about "How to convert BigQuery column to array".

目次

Prepare data

First, we have to prepare data.

Import this CSV data below as table array_sample.

key,val
a,1
a,2
a,3
b,2
b,4
b,5
c,3
c,2
c,-1

So 2-column-table would be created.

How to convert BigQuery column to array

In order to convert BigQuery column to array, we can use ARRAY_AGG().

ARRAY_AGG() is aggregate function that returns array.

You can use this function like below.

SELECT
key,
ARRAY_AGG(val) as val_array
FROM test.array_sample
GROUP BY key

This SQL returns result below.

Rowkeyval_array
1a1
2
3
2b2
4
5
3c3
2
-1
ARRAY_AGG

Values are aggregated to array by key.

Aggregate functions | BigQuery | Google Cloud

Conclusion

Today I explained about "How to convert BigQuery column to array".

In order to convert column to array in BigQuery, we can take this solution.

  • Use aggregate function ARRAY_AGG().

It is easy to understand because we can use GROUP BY clause.

この記事が気に入ったら
いいね または フォローしてね!

If you like this article, please share !
  • URLをコピーしました!
  • URLをコピーしました!

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

目次