Combine multiple rows into one space separated str

2019-02-06 06:35发布

So I have 5 rows like this

userid, col
--------------
1, a
1, b
2, c
2, d
3, e

How would I do query so it will look like this

userid, combined
1, a b
2, c d
3, e

标签: sql mysql hive
5条回答
劳资没心,怎么记你
2楼-- · 2019-02-06 07:10

Use the GROUP_CONCAT aggregate function:

  SELECT yt.userid,
         GROUP_CONCAT(yt.col SEPARATOR ' ') AS combined
    FROM YOUR_TABLE yt
GROUP BY yt.userid

The default separator is a comma (","), so you need to specify the SEPARATOR of a single space to get the output you desire.

If you want to ensure the order of the values in the GROUP_CONCAT, use:

  SELECT yt.userid,
         GROUP_CONCAT(yt.col ORDER BY yt.col SEPARATOR ' ') AS combined
    FROM YOUR_TABLE yt
GROUP BY yt.userid
查看更多
疯言疯语
3楼-- · 2019-02-06 07:14

In hive you can use

SELECT userid, collect_set(combined) FROM tabel GROUP BY user_id;

collect_set removes duplicated. If you need to keep them you can check this post:

COLLECT_SET() in Hive, keep duplicates?

查看更多
乱世女痞
4楼-- · 2019-02-06 07:16
SELECT 
  userid,
  concat_ws(" ", collect_set(col)) AS combined
FROM table 
GROUP BY userid
查看更多
beautiful°
5楼-- · 2019-02-06 07:17

I'm pretty sure that you can't do this using Hive QL. However, it should be possible to do so if you write your own Map/Reduce scripts - see this tutorial to get started.

查看更多
beautiful°
6楼-- · 2019-02-06 07:21
  1. MySQL with duplicates: select col1, group_concat(col2) from table1 group by col1
  2. MySQL without duplicates: select col1, group_concat(distinct col2) from table1 group by col1
  3. Hive with duplicates: select col1, collect_list(col2) from table1 group by col1
  4. Hive without duplicates: select col1, collect_set(col2) from table1 group by col1
查看更多
登录 后发表回答