Presto相当于MySQL group_concat

Mik*_*yer 12 mysql group-concat presto

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

MySQL的:

select 
  a,
  group_concat(b separator ',')
from table
group by a
Run Code Online (Sandbox Code Playgroud)

普雷斯托:

select 
  a,
  array_join(array_agg(b), ',')
from table
group by a
Run Code Online (Sandbox Code Playgroud)

(在搜索group_concat功能时,可以在此处找到建议的Presto解决方法.)

小智 16

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

select 
  a,
  array_join(array_agg(b), ',')
from table
group by a
Run Code Online (Sandbox Code Playgroud)

  • 对于那些在聚合中看到意外顺序的人,您可以使用 array_agg(x ORDER BY y) (3认同)

小智 7

另外,如果您仅在寻找唯一值(等效于)group_concat(distinct ... separator ', '),请尝试以下操作:

array_join(array_distinct(array_agg(...)), ', ')
Run Code Online (Sandbox Code Playgroud)


dub*_*ber 5

尽管已请求该功能,但截至此答案还没有任何功能。

您的问题中提到了最接近的等效项。

WITH tmp AS (
SELECT 'hey' AS str1
UNION ALL
SELECT ' there'
)
SELECT array_join(array_agg(str1), ',', '') AS joined
FROM tmp
Run Code Online (Sandbox Code Playgroud)