The relationship between ARRAY_AGG and UNNEST

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Everything you need to work effectively with nested and repeated data in BigQuery — ARRAY, STRUCT, UNNEST, ARRAY_AGG, and related functions.
Here's an interesting SQL problem, similar to one I had to solve the other day. It involves some of our favourite BigQuery ARRAYS, but also correlated subqueries. Say we have a table events that represents some events, together with the city and the ...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

title: "The relationship between ARRAY_AGG and UNNEST" seoTitle: "BigQuery ARRAY_AGG vs UNNEST: Opposite Operations Explained" seoDescription: "ARRAY_AGG packs rows into an ARRAY and UNNEST unpacks an ARRAY back into rows — they are inverse operations in BigQuery." datePublished: Sat Jun 08 2024 18:26:00 GMT+0000 (Coordinated Universal Time) cuid: clx6g4uh900000ajm1u87d5dg slug: the-relationship-between-arrayagg-and-unnest cover: https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/VhDgReMsz8w/upload/910ca1350e91fa8b0c28ce16e9acd9c9.jpeg tags: databases, google-cloud, sql, bigquery, data-engineering
If you're working with nested data in BigQuery, you've might've seen UNNEST, which helps 'unpack' arrays into individual rows.
But there's also ARRAY_AGG, which, if you haven't encountered it before, which takes all rows for your GROUP BY bucket and creates an ARRAY out of them.
So, in essence, ARRAY_AGG and UNNEST are doing the exact opposite of each other.
Check my previous posts on the topic:
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.
Enjoyed this? Here are some related articles you might find useful: