【问题标题】:Presto equivalent of MySQL group_concat相当于 MySQL group_concat 的 Presto
【发布时间】:2017-10-23 20:27:03
【问题描述】:

我是 Presto 的新手,希望获得与 MySQL 中的 group_concat 函数相同的功能。以下两个是等价的吗?如果没有,关于如何在 Presto 中重新创建 group_concat 功能的任何建议?

MySQL:

select 
  a,
  group_concat(b separator ',')
from table
group by a

快速:

select 
  a,
  array_join(array_agg(b), ',')
from table
group by a

(在搜索 group_concat 功能时发现这是建议的 Presto 解决方法here。)

【问题讨论】:

    标签: mysql presto group-concat trino


    【解决方案1】:

    尝试在 Presto 中使用它代替 group_concat ::

    select 
      a,
      array_join(array_agg(b), ',')
    from table
    group by a
    

    【讨论】:

    • 聚合中看到意外顺序的可以使用array_agg(x ORDER BY y)
    【解决方案2】:

    另外,如果您只寻找唯一值 - 相当于 group_concat(distinct ... separator ', ') - 试试这个:

    array_join(array_distinct(array_agg(...)), ', ')
    

    【讨论】:

    • 我发现(在这种实现 distinct group_concat 的特殊情况下)使用嵌套查询手动获取 DISTINCT 值比 array_distinct 执行得更好 UDF。不确定这是否普遍正确虽然
    【解决方案3】:

    这个答案没有功能,though the feature has been requested

    您的问题中提到了最接近的等价物。

    WITH tmp AS (
    SELECT 'hey' AS str1
    UNION ALL
    SELECT ' there'
    )
    SELECT array_join(array_agg(str1), ',', '') AS joined
    FROM tmp
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-05
      • 2020-05-23
      • 2018-08-06
      • 2020-11-27
      • 1970-01-01
      • 2013-06-23
      • 2011-06-26
      • 2012-02-21
      相关资源
      最近更新 更多