【问题标题】:Google Cloud - BigQuery - Transposing 2 String Columns within a StructGoogle Cloud - BigQuery - 在结构中转置 2 个字符串列
【发布时间】:2021-06-01 15:56:38
【问题描述】:

大家早上好,

BigQuery 新手,我在过去使用过的其他 BI 语言中发现了一些非常直观的东西,即转置键和值,以便键成为列名,值成为与列关联的值.

我在网上查看了一些示例,当我处理它们时,我遇到了许多我无法解决的错误,因为我没有足够的经验。

对于那些不可避免地质疑我为什么要这样做的人,这是我有义务满足的业务要求,但是如果您对解决此问题的更好方法有任何想法,我非常乐意接受此建议并欣赏它。

**The Json:**

[
  {
    "resultsMap": {
      "map": {
        "item": [
          {
            "value": "2021-03-02-12.16.28.012279",
            "key": "TIMESTAMP"
          },
          {
            "value": "1",
            "key": "CONTROL_KEY"
          },
          {
            "value": "123465789",
            "key": "LOGGING_KEY"
          }
        ],
        "key": "5049"
      }
    }
  }
]

我有什么

resultsMap.map.key resultsMap.map.item.value resultsMap.map.item.key
5049 TIMESTAMP 2021-03-02-12.16.28.012279
CONTROL_KEY 1
LOGGING_KEY 123456789

我想要什么

resultsMap.map.key TIMESTAMP CONTROL_KEY LOGGING_KEY
5049 2021-03-02-12.16.28.012279 1 123456789

非常感谢您的帮助。

问候, 斯科特

【问题讨论】:

    标签: arrays struct google-bigquery


    【解决方案1】:

    对于这种情况,我们必须手动从数组中选择:

    with mytable as (
      select struct( struct(5049 as key, [struct("2021-03-02-12.16.28.012279" as value, "TIMESTAMP" as key), struct("1" as value, "CONTROL_KEY" as key), struct("123465789" as value, "LOGGING_KEY" as key)] as item) as map) as resultsMap
    )
    select
      resultsMap.map.key,
      (select value from unnest(resultsMap.map.item) where key = 'TIMESTAMP') as TIMESTAMP,
      (select value from unnest(resultsMap.map.item) where key = 'CONTROL_KEY') as CONTROL_KEY,
      (select value from unnest(resultsMap.map.item) where key = 'LOGGING_KEY') as LOGGING_KEY
    from mytable
    

    【讨论】:

      【解决方案2】:

      也考虑下面(假设所有要提取的键都存在):

      select resultsMap.map.key,
        kv[offset(0)] as TIMESTAMP, 
        kv[offset(1)] as CONTROL_KEY, 
        kv[offset(2)] as LOGGING_KEY, 
      from `project.dataset.table` t, 
      unnest([(
        select as struct array_agg(value 
          order by case key 
            when 'TIMESTAMP' then 0 
            when 'CONTROL_KEY' then 1 
            when 'LOGGING_KEY' then 2 
          end) as kv 
        from t.resultsMap.map.item 
      )]) 
      

      还有进一步重构的版本:

      select resultsMap.map.key,
        kv[offset(2)] as TIMESTAMP, 
        kv[offset(0)] as CONTROL_KEY, 
        kv[offset(1)] as LOGGING_KEY, 
      from `project.dataset.table` t,
      unnest([(
        select as struct array_agg(value 
          order by range_bucket(key, ['CONTROL_KEY','LOGGING_KEY','TIMESTAMP'])
        ) as kv 
        from t.resultsMap.map.item 
      )])       
      

      如果应用于您问题中的样本数据 - 输出是

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-22
        • 2022-06-10
        • 2015-11-07
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多