【问题标题】:extracting name/value pairs into columns and values - Shopify Data In Bigquery将名称/值对提取到列和值中 - Bigquery 中的 Shopify 数据
【发布时间】:2022-01-01 13:51:53
【问题描述】:

我们使用 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_interval_frequency
  • charge_on_day_of_month
  • subscription_id
  • number_charges_until_expiration
  • shipping_interval_frequency

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

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

【问题讨论】:

    标签: arrays json google-bigquery shopify fivetran


    【解决方案1】:

    考虑以下方法

    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'
    ))
    

    【讨论】:

    • 谢谢!这完成了工作并教会了我一些新技巧。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2023-03-12
    相关资源
    最近更新 更多