0

我们使用 FiveTran 从 shopify 中提取数据并将其存储在 BigQuery 中。order_line 表中的“属性”字段包含看起来像键/值对数组的内容。在这种情况下,名称/值。字段类型是字符串,这里是内容示例

order_line_id   properties
9956058529877   [{"name":"_order_bump_rule_id","value":"4afx7cbw6"},{"name":"_order_bump_bump_id","value":"769d1996-b6fb-4bc3-8d41-c4d7125768c5"},{"name":"_source","value":"order-bump"}]
4467731660885   [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_delay","value":null},{"name":"charge_on_day_of_week","value":null},{"name":"charge_interval_frequency","value":null},{"name":"charge_on_day_of_month","value":null},{"name":"shipping_interval_frequency","value":null},{"name":"number_charges_until_expiration","value":null}]
4467738738773   [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_delay","value":null},{"name":"charge_on_day_of_week","value":null},{"name":"charge_interval_frequency","value":null},{"name":"charge_on_day_of_month","value":null},{"name":"shipping_interval_frequency","value":null},{"name":"number_charges_until_expiration","value":null}]
4578798600277   [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_interval_frequency","value":null},{"name":"shipping_interval_frequency","value":null}]

我正在尝试编写一个查询,该查询为每条记录生成一行,其中每个名称值都有一列:

  • shipping_interval_unit_type
  • Charge_on_day_of_week
  • 充电间隔频率
  • Charge_on_day_of_month
  • 订阅 ID
  • number_charges_until_expiration
  • shipping_interval_frequency

和相应的“价值”。此字段“属性”可以包含许多不同的“名称”值,并且它们每次可以以不同的顺序排列。上面提到的“名称”值并不总是出现在“属性”字段中。

我已经尝试过 json 函数,但它似乎没有为 json 正确格式化。我试过取消嵌套,但失败了,因为它是一个字符串。

4

1 回答 1

0

考虑以下方法

select * from (
  select order_line_id, 
    json_extract_scalar(property, '$.name') name,
    json_extract_scalar(property, '$.value') value
  from your_table, unnest(json_extract_array(properties)) property
)
pivot (min(value) for name in (
  'shipping_interval_unit_type',
  'charge_on_day_of_week',
  'charge_interval_frequency',
  'charge_on_day_of_month',
  'subscription_id',
  'number_charges_until_expiration',
  'shipping_interval_frequency'
))
于 2021-11-23T03:43:50.000 回答