来自数组中 key:value 对的 StandardSQL BigQuery 值分为单独的列。如何?

StandardSQL BigQuery values from key:value pairs within an Array into separate columns. How?

我有一个带有数组列的 BigQuery table,多个(1 到 4)key:value 对由竖线“|”分隔。我想拉 key:value 对并添加额外的列,其中 'key' 作为列 header 和 'value' 以及......以及 value/entry.

然而,虽然有统一的“键”,但它们并非都按相同的顺序放置,因此按顺序拆分并不能很好地工作。我环顾四周并探索了“JSON_EXTRACT_SCALAR”和“UNNEST”(来自 question/answer:),但无法获得预期的结果。我也探索过使用“OFFSET”,但不知道如何将它们组合在一起。

这是数据字段(它们的数组)的样子:

Row campaignLabels  
1   Segment: Rivers Non-Brand | Strategy: All Else | Category: Non-Brand | CN:Pause_5-29-19
2   Segment: Rivers Non-Brand | Category: Non-Brand | Strategy: All Else | CN:Pause_5-29-19
3   Category: Upper Funnel | Strategy: All Else
4   Strategy: All Else | Segment: Rivers Brand | Category: Brand
5   Strategy: All Else | Category: Brand | Segment: Rivers Brand
6   Segment: Rivers Non-Brand | Category: Non-Brand | Strategy: All Else
7   Strategy: All Else | Segment: Viking Other Brand | Category: Brand
8   Strategy: All Else | Category: Brand | Segment: Rivers Brand
9   Strategy: All Else | Category: Brand | Segment: Rivers Brand
10  Strategy: All Else | Category: Brand | Segment: Viking Other Brand

理想的输出是查询同样的 table 提取某些列并添加列,其中“策略”、“类别”和“细分”作为列标签,值作为返回值。

求助!

一些尝试让我部分成功但没有得到所需的结果:

SELECT 
  DISTINCT(SUBSTR(Part1, 10)) AS Strategy
FROM (
  SELECT
    Labels[OFFSET(0)] AS Part1,
    Labels[OFFSET(1)] AS Part2,
    Labels[SAFE_OFFSET(2)] AS Part3,
    Labels[SAFE_OFFSET(3)] AS Part4
  FROM (
    SELECT
      SPLIT(campaignLabels,"| ") AS Labels
    FROM
      `table_A` )
  )
WHERE Part1 LIKE "Strategy:%"

以下适用于 BigQuery 标准 SQL

#standardSQL
select campaignLabels, 
  ( select as struct
      max(if(key = 'Segment', value, null)) as Segment,
      max(if(key = 'Strategy', value, null)) as Strategy,
      max(if(key = 'Category', value, null)) as Category
    from (
      select as struct kv[offset(0)] as key, trim(kv[offset(1)]) as value
      from t.labels label, 
      unnest([struct(split(label, ':') as kv)])
    )
  ).*
from `project.dataset.table`, 
unnest([struct(split(campaignLabels, ' | ') as labels)]) t    

如果将我们的问题应用于样本数据 - 输出是