{"id":43754,"date":"2023-08-28T03:09:10","date_gmt":"2023-08-28T01:09:10","guid":{"rendered":"https:\/\/blog.sheetgo.com\/?p=43754"},"modified":"2025-12-17T18:44:12","modified_gmt":"2025-12-17T17:44:12","slug":"como-usar-o-unnest-no-bigquery","status":"publish","type":"post","link":"https:\/\/www.sheetgo.com\/pt\/blog\/data-science\/how-to-use-unnest-in-bigquery\/","title":{"rendered":"Como usar o unnest no BigQuery"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221; da_is_popup=&#8221;off&#8221; da_exit_intent=&#8221;off&#8221; da_has_close=&#8221;on&#8221; da_alt_close=&#8221;off&#8221; da_dark_close=&#8221;off&#8221; da_not_modal=&#8221;on&#8221; da_is_singular=&#8221;off&#8221; da_with_loader=&#8221;off&#8221; da_has_shadow=&#8221;on&#8221; da_disable_devices=&#8221;off|off|off&#8221;][et_pb_row _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>UNNEST in BigQuery is a very useful function that allows you to flatten nested and repeated data structures for easier analysis. When dealing with complex hierarchical data like arrays, UNNEST comes to the rescue.<\/p>\n<p>Unlike traditional tabular data, these arrays contain nested and repeated data, which can make aggregation and joins more difficult. When this happens, UNNEST allows you to expand nested or repeated data into separate rows so you can analyze the unnested elements much more easily.<\/p>\n<p>In this article, we&#8217;ll explore the UNNEST function in BigQuery to see how it can simplify nested data analysis.\u00a0<\/p>\n<p>[\/et_pb_text][et_pb_text module_id=&#8221;nested&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2>Understanding nested and repeated data structures in BigQuery<\/h2>\n<p>When analyzing large and complex datasets in BigQuery, it&#8217;s common to encounter nested and repeated fields. These nested data structures allow you to store multiple values in a single field in BigQuery, making it easier to manipulate multiple values at once.<\/p>\n<p>When you look at the table below, you will notice that the column with the &#8220;event parameters&#8221; contains subcolumns with multiple values, which are all part of the same row.\u00a0<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2023\/08\/Unnest-BigQuery-1.png&#8221; alt=&#8221;Unnest BigQuery 1&#8243; title_text=&#8221;Unnest BigQuery 1&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; width=&#8221;100%&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>If you examine the table schema, you&#8217;ll see this column&#8217;s data type isn&#8217;t an integer, string, float, or boolean. Rather, it&#8217;s defined as a record since it contains subcolumns.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2023\/08\/Unnest-BigQuery-2.png&#8221; alt=&#8221;Unnest BigQuery 2&#8243; title_text=&#8221;Unnest BigQuery 2&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; width=&#8221;100%&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>As you can see, the &#8220;event_params&#8221; field has subcolumns (&#8220;key&#8221; and &#8220;value&#8221;) with multiple values inside. Notice that BigQuery supports nested data with subcolumns within subcolumns. In this table, &#8220;value&#8221; contains multiple subcolumns.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2023\/08\/Unnest-BigQuery-3-1.png&#8221; alt=&#8221;Unnest BigQuery 3&#8243; title_text=&#8221;Unnest BigQuery 3&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; width=&#8221;100%&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text module_id=&#8221;how&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2>How does UNNEST work in BigQuery?<\/h2>\n<p>The UNNEST function in BigQuery is used to flatten nested or repeated data structures into separate rows. What it does is take as input a column with a nested data type like an ARRAY, and expand the nested or repeated elements into multiple rows. As a result, it will flatten the data into a tabular structure.<\/p>\n<p>After UNNEST transforms the nested or repeated data into a flat table, you can query columns directly and analyze its individual elements.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2023\/08\/BigQuery-unnest.png&#8221; title_text=&#8221;BigQuery unnest&#8221; align=&#8221;center&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; width=&#8221;100%&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text module_id=&#8221;use&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<h2>How to use UNNEST in BigQuery<\/h2>\n<p>In order to flatten nested data structures in BigQuery, we&#8217;ll use the UNNEST function. Here&#8217;s the UNNEST syntax:<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.21.2&#8243; border_width_left=&#8221;4px&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>SELECT column1, column2, \u2026<\/p>\n<p>FROM table_name, UNNEST(column) AS alias\u00a0<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>We&#8217;ll use the UNNEST function to break the &#8220;event_params&#8221; column down into separated rows.<\/p>\n<p>[\/et_pb_text][et_pb_text module_class=&#8221;spreadsheet-function&#8221; _builder_version=&#8221;4.21.2&#8243; border_width_left=&#8221;4px&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>SELECT ga.event_name, params.key, params.value<\/p>\n<p>FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_20210131` AS ga,<\/p>\n<p>UNNEST(event_params) AS params<\/p>\n<p>LIMIT 1000;<\/p>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>Let&#8217;s take a closer look at the query.<\/p>\n<p><b>SELECT ga.event_name, params.key, params.value<\/b><\/p>\n<p>First, I&#8217;ve specified the columns I want BigQuery to return: event_name, params.key, and params.value. &#8220;Params&#8221; is the alias I&#8217;ll assign when I write the UNNEST function.<\/p>\n<p><b>FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_20210131` AS ga,<\/b><\/p>\n<p>This is the table I want to query. I&#8217;ve used an AS statement to assign an alias to the table: &#8220;ga&#8221;.<\/p>\n<p><b>UNNEST(event_params) AS params<\/b><\/p>\n<p>The UNNEST function will expand the nested data into separate rows.<\/p>\n<p><b>LIMIT 1000;<\/b><\/p>\n<p>This clause limits the number of rows returned to 1000.<\/p>\n<p>[\/et_pb_text][et_pb_image src=&#8221;https:\/\/static.sheetgo.com\/wp-content\/uploads\/2023\/08\/Unnest-BigQuery-4.png&#8221; alt=&#8221;Unnest BigQuery 4&#8243; title_text=&#8221;Unnest BigQuery 4&#8243; align=&#8221;center&#8221; _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; width=&#8221;100%&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;][\/et_pb_image][et_pb_text _builder_version=&#8221;4.21.2&#8243; _module_preset=&#8221;default&#8221; global_colors_info=&#8221;{}&#8221; theme_builder_area=&#8221;post_content&#8221;]<\/p>\n<p>BigQuery has expanded the data into separate rows, flattening the nested data.<\/p>\n<p>This is how you can unnest data in BigQuery. If you want to learn how to summarize and analyze data in BigQuery, check out this article on <a href=\"https:\/\/www.sheetgo.com\/blog\/integrations\/how-to-use-bigquery-aggregate-functions\/\">how to use aggregate functions<\/a>.<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UNNEST in BigQuery is a very useful function that allows you to flatten nested and repeated data structures for easier analysis. When dealing with complex hierarchical data like arrays, UNNEST comes to the rescue. Unlike traditional tabular data, these arrays contain nested and repeated data, which can make aggregation and joins more difficult. When this [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":43847,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[31],"tags":[],"class_list":["post-43754","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science"],"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/posts\/43754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/comments?post=43754"}],"version-history":[{"count":0,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/posts\/43754\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/media\/43847"}],"wp:attachment":[{"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/media?parent=43754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/categories?post=43754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheetgo.com\/pt\/wp-json\/wp\/v2\/tags?post=43754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}