将多行合并为一个空格分隔的字符串
所以我有 5 行像这样
userid, col
--------------
1, a
1, b
2, c
2, d
3, e
我将如何进行查询,所以它看起来像这样
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MySQL
包含重复项:select col1, group_concat(col2) from table1 group by col1
MySQL
不包含重复项:select col1, group_concat(distinct col2) from table1 group by col1
Hive
包含重复项:select col1,collect_list(col2) from table1 group by col1
Hive
without重复:select col1,collect_set(col2) from table1 group by col1
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
使用GROUP_CONCAT聚合函数 :
默认分隔符是逗号(“,”),因此您需要指定单个空格的分隔符才能获得您想要的输出。
如果要确保 GROUP_CONCAT 中值的顺序,请使用:
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:
在hive中你可以使用
collect_set删除重复的。如果您需要保留它们,可以查看这篇文章:
COLLECT_SET() in Hive, keep duplicates ?
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?