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
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
Use the GROUP_CONCAT aggregate function:
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:
In hive you can use
collect_set removes duplicated. If you need to keep them you can check this post:
COLLECT_SET() in Hive, keep duplicates?
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.
MySQL
with duplicates:select col1, group_concat(col2) from table1 group by col1
MySQL
without duplicates:select col1, group_concat(distinct col2) from table1 group by col1
Hive
with duplicates:select col1, collect_list(col2) from table1 group by col1
Hive
without duplicates:select col1, collect_set(col2) from table1 group by col1