如何以有序的不同值将多行连接为单行?

时间:2019-10-30 10:33:26

标签: sql google-bigquery

我想使用bigquery将多行连接成单行

我有这样的桌子

A           |    B    |   C     |   D      |
09-10-2019  |    1    |   math  |  social  |
09-10-2019  |    1    |   math  |  science |
09-10-2019  |    1    |   math  |  science |

我想要这样的结果表

A           |    B    |   C     |   D               |
09-10-2019  |    1    |   math  |  science, social  |

但是我的代码得到了这样的结果

A           |    B    |   C               |        D                   |
09-10-2019  |    1    |   math,math,math  |  science, science, social  |

我正在使用

下订单
group_concat(unique(C),",") over(partition by A, B ORDER BY C)
group_concat(unique(D),",") over(partition by A, B ORDER BY D)

但我遇到了错误

2 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT a, b, 
  (SELECT STRING_AGG(c ORDER BY c) FROM UNNEST(c_distinct) c) c,
  (SELECT STRING_AGG(d ORDER BY d) FROM UNNEST(d_distinct) d) d
FROM (
  SELECT a, b,
    ARRAY_AGG(DISTINCT c) AS c_distinct,
    ARRAY_AGG(DISTINCT d) AS d_distinct
  FROM `project.dataset.table`
  GROUP BY a, b
)

您可以使用问题中的示例数据来测试,玩转上面的示例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '09-10-2019' a, 1 b, 'math' c, 'social' d UNION ALL
  SELECT '09-10-2019', 1, 'math', 'science' UNION ALL
  SELECT '09-10-2019', 1, 'math', 'science' 
)
SELECT a, b, 
  (SELECT STRING_AGG(c ORDER BY c) FROM UNNEST(c_distinct) c) c,
  (SELECT STRING_AGG(d ORDER BY d) FROM UNNEST(d_distinct) d) d
FROM (
  SELECT a, b,
    ARRAY_AGG(DISTINCT c) AS c_distinct,
    ARRAY_AGG(DISTINCT d) AS d_distinct
  FROM `project.dataset.table`
  GROUP BY a, b
)   

有结果

Row a           b   c       d    
1   09-10-2019  1   math    science,social   

答案 1 :(得分:0)

使用string_agg()

select a, b,
       string_agg(c, ',' order by c) as cs,
       string_agg(d, ',' order by d) as ds
from t
group by a, b;

也就是说,我建议使用数组:

select a, b,
       array_agg(c order by c) as cs,
       array_agg(d order by d) as ds
from t
group by a, b;