【问题标题】:BigQuery: How to group similar records of an array together into comma separated field?BigQuery:如何将数组的相似记录组合成逗号分隔的字段?
【发布时间】:2021-10-14 14:50:19
【问题描述】:

我有一个以逗号分隔的文件的文本字段。记录是这样的

SKU                  Info
123                Common:  tshirt
345                common:  jeans, color: green
567                common:  tshirt, common: jeans, color: blue

我正在尝试将它们放入数组中,但将第一个单词相同的位置结合起来。 IE:对于上面的记录 3 (567),数组字段将是

  common:  tshirt, jeans
  color: blue

我当前的查询:

 select   array(select trim(val) from unnest(split(trim(infos, ''))) val) as testing

返回

567    common: jeans
567    common: tshirt
567    color:  blue

任何见解或帮助将不胜感激。谢谢!

【问题讨论】:

    标签: arrays google-bigquery


    【解决方案1】:

    考虑以下方法

    select sku, array(
        select any_value(split(trim(val), ':')[offset(0)]) || ': ' ||
          string_agg(split(trim(val), ':')[offset(1)], ', ') 
        from unnest(split(trim(info, ''))) val
        group by split(trim(val), ':')[offset(0)]
      ) as testing
    from your_table             
    

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

    【讨论】:

    • 谢谢!那行得通!虽然我稍微修改了它,但必须摆脱空值并执行 "Safe_offset" { , array( (select any_value(split(trim(val), ':')[safe_offset(0)]) || ': ' | | string_agg(split(trim(val), ':')[safe_offset(1)], ', ') from unnest(split(trim(infos, ''))) val WHERE regexp_extract(val, r'^(. *?)\:') IS NOT NULL group by split(trim(val), ':')[safe_offset(0)]) ) as infosArray}
    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2015-09-04
    • 2021-12-20
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多