{"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":"comment-utiliser-unnest-dans-bigquery","status":"publish","type":"post","link":"https:\/\/www.sheetgo.com\/fr\/blog\/data-science\/how-to-use-unnest-in-bigquery\/","title":{"rendered":"Comment utiliser unnest dans 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><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this article, we&#8217;ll explore the UNNEST function in BigQuery to see how it can simplify nested data analysis.\u00a0<\/span><\/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><span style=\"font-weight: 400;\">Understanding nested and repeated data structures in BigQuery<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">When analyzing large and complex datasets in BigQuery, it&#8217;s common to encounter nested and repeated fields. <\/span><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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<\/span><\/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><span style=\"font-weight: 400;\">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.<\/span><\/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><span style=\"font-weight: 400;\">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.<\/span><\/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><span style=\"font-weight: 400;\">How does UNNEST work in BigQuery?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After UNNEST transforms the nested or repeated data into a flat table, you can query columns directly and analyze its individual elements.<\/span><\/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><span style=\"font-weight: 400;\">How to use UNNEST in BigQuery<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">In order to flatten nested data structures in BigQuery, we&#8217;ll use the UNNEST function. Here&#8217;s the UNNEST syntax:<\/span><\/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><span style=\"font-weight: 400;\">SELECT column1, column2, \u2026<\/span><\/p>\n<p><span style=\"font-weight: 400;\">FROM table_name, UNNEST(column) AS alias\u00a0<\/span><\/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><span style=\"font-weight: 400;\">We&#8217;ll use the UNNEST function to break the &#8220;event_params&#8221; column down into separated rows.<\/span><\/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><span style=\"font-weight: 400;\">SELECT ga.event_name, params.key, params.value<\/span><\/p>\n<p><span style=\"font-weight: 400;\">FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_20210131` AS ga,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">UNNEST(event_params) AS params<\/span><\/p>\n<p><span style=\"font-weight: 400;\">LIMIT 1000;<\/span><\/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><span style=\"font-weight: 400;\">Let&#8217;s take a closer look at the query.<\/span><\/p>\n<p><b>SELECT ga.event_name, params.key, params.value<\/b><\/p>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><b>FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_20210131` AS ga,<\/b><\/p>\n<p><span style=\"font-weight: 400;\">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;.<\/span><\/p>\n<p><b>UNNEST(event_params) AS params<\/b><\/p>\n<p><span style=\"font-weight: 400;\">The UNNEST function will expand the nested data into separate rows.<\/span><\/p>\n<p><b>LIMIT 1000;<\/b><\/p>\n<p><span style=\"font-weight: 400;\">This clause limits the number of rows returned to 1000.<\/span><\/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><span style=\"font-weight: 400;\">BigQuery has expanded the data into separate rows, flattening the nested data.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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 <\/span><a href=\"https:\/\/www.sheetgo.com\/blog\/integrations\/how-to-use-bigquery-aggregate-functions\/\"><span style=\"font-weight: 400;\">how to use aggregate functions<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/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":[],"_links":{"self":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts\/43754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/comments?post=43754"}],"version-history":[{"count":0,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/posts\/43754\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/media\/43847"}],"wp:attachment":[{"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/media?parent=43754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/categories?post=43754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheetgo.com\/fr\/wp-json\/wp\/v2\/tags?post=43754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}