在 BigQuery 中分组连接字符串(行)

Group Concatenate Strings (Rows) in BigQuery

我正在使用 Google BigQuery 并且我有一个如下所示的查询:

 SELECT
        prod.abc
        uniqueid,
        variable2,
        cust.variable1,
        purch.variable2,
        from mydata.order
        left join
        UNNEST(purchases) as purch,
        UNNEST(codes_abs) as cod, UNNEST(cod.try_products) as prod

当我这样做时,结果是 table 看起来像这样:

    |prod.abc| uniqueid | variable2 | ...|
    |APP123  | customer1| value     | ...|
    |BLU155  | customer1| value     | ...|
    |TRI134  | customer1| value     | ...|
    |LO123   | customer2| value     | ...|
    |ZU9274  | customer2| value     | ...|
    |TO134   | customer3| value     | ...|

我想做的是连接“prod.abc”列中的值,将它们按“uniqueid”分组并用“,”分隔。然而,我在网上找到了许多解决方案,因为我在查询中取消了其他变量的嵌套,所以我找到的 none 解决方案似乎适用于我的情况。这些值不需要以任何方式排序。基本上,我想结束的是:

    |prod.abc                  | uniqueid | variable2 | ...|
    |APP123, BLU155, TRI134    | customer1| value     | ...|
    |LO123, ZU9274             | customer2| value     | ...|
    |TO134                     | customer3| value     | ...|

像这样保留重复项的 table 也可以,因为我稍后可以删除它们:

|prod.abc                  | uniqueid | variable2 | ...|
|APP123, BLU155, TRI134    | customer1| value     | ...|
|APP123, BLU155, TRI134    | customer1| value     | ...|
|APP123, BLU155, TRI134    | customer1| value     | ...|
|LO123, ZU9274             | customer2| value     | ...|
|LO123, ZU9274             | customer2| value     | ...|
|TO134                     | customer3| value     | ...|

非常感谢任何帮助。谢谢!

分别取消嵌套: 聚合有用吗?

SELECT STRING_AGG(item.abc, ',')
       uniqueid, variable2, cust.variable1, purch.variable2
FROM mydata.order LEFT JOIN
     UNNEST(purchases) as purch
     ON true LEFT JOIN
     UNNEST(codes_abs) as cod
     ON true LEFT JOIN
     UNNEST(cod.try_items) as item
     ON true
GROUP BY uniqueid, variable2, cust.variable1, purch.variable2;

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT
  STRING_AGG(prod.abc, ', ') AS abc
  uniqueid,
  variable2,
  cust.variable1,
  purch.variable2,
FROM mydata.order
LEFT JOIN UNNEST(purchases) AS purch
LEFT JOIN UNNEST(codes_abs) AS cod
LEFT JOIN UNNEST(cod.try_products) AS prod
GROUP BY uniqueid,
  variable2,
  cust.variable1,
  purch.variable2