Presto equivalent of MySQL group_concat

南笙酒味 提交于 2020-06-09 10:05:08

问题


I'm new to Presto and looking to get the same functionality as the group_concat function in MySQL. Are the following two equivalent? If not, any suggestions for how I can recreate the group_concat functionality in Presto?

MySQL:

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

Presto:

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

(Found this as a suggested Presto workaround here when searching group_concat functionality.)


回答1:


Try using this in place of group_concat in Presto ::

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



回答2:


Also, if you're looking for unique values only – an equivalent to group_concat(distinct ... separator ', ') – try this:

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



回答3:


There's no function as of this answer, though the feature has been requested.

The closest equivalent is mentioned in your question.

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


来源:https://stackoverflow.com/questions/44142356/presto-equivalent-of-mysql-group-concat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!