按唯一列对SQL结果进行分组,并将其余部分添加到列表中

时间:2017-02-07 21:33:52

标签: sql google-bigquery

我有一个表查询结果,看起来像这样(在几个SQL查询之后):

Element Subelement  ID          Email               Value 

1003022 10003981    "454255"    "email1@yahoo.com"  25.5     
1003022 10003981    "454109"    "email2@yahoo.com"  34.45    
1003027 10033987    "454369"    "email3@yahoo.com"  1.9  
1003027 10033987    "454255"    "email1@yahoo.com"  25.5     
1003011 10021233    "454209"    "email2@yahoo.com"  34.45
1222011 13513544    "454209"    "email2@yahoo.com"  34.45

这些是前两列不同的一些事件,作为一个组。 Col1与Col 2。

基于ID(电子邮件和值对于一个ID是相同的),我希望得到如下结果:

ID          Email               Value   Elements

"454255"    "email1@yahoo.com"  25.5    {[1003022, 10003981], [1003027, 10033987]} 
"454109"    "email2@yahoo.com"  34.45   {[1003022, 10003981], [1003011, 10021233], [1222011, 13513544]} 
"454369"    "email3@yahoo.com"  1.9     {[1003027, 10033987]}

或者将ID(电子邮件,值)保留在一行上的任何格式,并将Element和Subelement添加到列表/数组中。

更新: 我已经尝试了 group_concat ,但找不到办法。

3 个答案:

答案 0 :(得分:1)

这个怎么样?

#standardSQL
SELECT ID, email, value, ARRAY_AGG(STRUCT(element, subelement)) AS Elements
FROM YourTable
GROUP BY ID, email, value;

答案 1 :(得分:0)

使用标准SQL,您可以执行以下操作:

#standardSQL
with t as 
(select 1003022 element, 10003981 subelement, "454255" id, "email1@yahoo.com" email, 25.5 value union all
 select 1003027, 10033987,    "454255",    "email1@yahoo.com",  25.5)     
SELECT id, email, value, 
       array_agg(struct<array<int64>>([element, subelement])) elements 
FROM t
GROUP BY 1, 2, 3

答案 2 :(得分:0)

#standardSQL
WITH yourTable AS (
  SELECT 1003022 AS Element, 10003981 AS Subelement, "454255" AS ID, "email1@yahoo.com" AS Email, 25.5 AS Value UNION ALL     
  SELECT 1003022 AS Element, 10003981 AS Subelement, "454209" AS ID, "email2@yahoo.com" AS Email, 34.45 AS Value UNION ALL    
  SELECT 1003027 AS Element, 10033987 AS Subelement, "454369" AS ID, "email3@yahoo.com" AS Email, 1.9 AS Value UNION ALL  
  SELECT 1003027 AS Element, 10033987 AS Subelement, "454255" AS ID, "email1@yahoo.com" AS Email, 25.5 AS Value UNION ALL     
  SELECT 1003011 AS Element, 10021233 AS Subelement, "454209" AS ID, "email2@yahoo.com" AS Email, 34.45 AS Value UNION ALL
  SELECT 1222011 AS Element, 13513544 AS Subelement, "454209" AS ID, "email2@yahoo.com" AS Email, 34.45 AS Value 
)     
SELECT 
  ID, Email, Value, 
  CONCAT('{', STRING_AGG(CONCAT('[', CAST(Element AS STRING), ',', CAST(Subelement AS STRING), ']')), '}') AS Elements
FROM yourTable
GROUP BY ID, Email, Value
-- ORDER BY Email

结果是

ID      Email               Value   Elements     
454255  email1@yahoo.com    25.5    {[1003022,10003981],[1003027,10033987]}  
454209  email2@yahoo.com    34.45   {[1003022,10003981],[1003011,10021233],[1222011,13513544]}   
454369  email3@yahoo.com    1.9     {[1003027,10033987]}

你的问题在预期输出意义上有点模糊,因此你有很多答案 我认为 - 越多越好:o)