Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presto equivalent of MySQL group_concat

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.)

like image 572
Mike Moyer Avatar asked May 23 '17 18:05

Mike Moyer


People also ask

What is Group_concat in MySQL?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

Does SQL Server have Group_concat?

The SQL Server Equivalent to GROUP_CONCAT() This function allows you to return a result set as a comma-separated list, as opposed to listing each row as a separate row (as with a normal result set).


2 Answers

Try using this in place of group_concat in Presto ::

select    a,   array_join(array_agg(b), ',') from table group by a 
like image 175
Rahul Ahuja Avatar answered Sep 27 '22 22:09

Rahul Ahuja


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

array_join(array_distinct(array_agg(...)), ', ') 
like image 20
Jacob Rose Avatar answered Sep 27 '22 22:09

Jacob Rose