【发布时间】:2018-10-11 20:10:00
【问题描述】:
下面是我的 BigQuery 表的架构。我正在选择 sentence_id、store 和 BU_model,并将数据插入 BigQuery 中的另一个表。生成的新表的数据类型分别为整数、重复和重复。 我想展平/取消嵌套重复的字段,以便在我的第二个表中将它们创建为 STRING 字段。如何使用标准 sql 来实现?
+- sentences: record (repeated)
| |- sentence_id: integer
| |- autodetected_language: string
| |- processed_language: string
| +- attributes: record
| | |- agent_rating: integer
| | |- store: string (repeated)
| +- classifications: record
| | |- BU_Model: string (repeated)
我用来创建第二个表的查询如下。我想将 BU_Model 作为 STRING 列进行查询。
SELECT sentence_id ,a.attributes.store,a.classifications.BU_Model
FROM staging_table , unnest(sentences) a
预期输出应如下所示:
暂存表:
41783851 regions Apparel
district Footwear
12864656 regions
district
最终目标表:
41783851 regions Apparel
41783851 regions Footwear
41783851 district Apparel
41783851 district Footwear
12864656 regions
12864656 district
我尝试了以下查询,它似乎按预期工作,但这意味着我必须取消嵌套每个预期的重复字段。我在 Bigquery 中的表有 50 多个重复的列。有没有更简单的方法解决这个问题?
SELECT
sentence_id,
flattened_stores,
flattened_Model
FROM `staging`
left join unnest(sentences) a
left join unnest(a.attributes.store) as flattened_stores
left join unnest(a.classifications.BU_Model) as flattened_Model
【问题讨论】:
标签: google-cloud-platform google-bigquery