所以我有5行这样
userid, col
--------------
1, a
1, b
2, c
2, d
3, e
我将如何进行查询,使其看起来像这样
userid, combined
1, a b
2, c d
3, e
在蜂巢中,您可以使用
SELECT userid, collect_set(combined) FROM tabel GROUP BY user_id;
collect_set 删除重复的。如果您需要保留它们,可以查看此帖子:
SELECT yt.userid,
GROUP_CONCAT(yt.col SEPARATOR ' ') AS combined
FROM YOUR_TABLE yt
GROUP BY yt.userid
默认分隔符是逗号 (","),因此您需要指定单个空格的 SEPARATOR 以获得您想要的输出。
如果要确保 GROUP_CONCAT 中值的顺序,请使用:
SELECT yt.userid,
GROUP_CONCAT(yt.col ORDER BY yt.col SEPARATOR ' ') AS combined
FROM YOUR_TABLE yt
GROUP BY yt.userid
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
没有重复:select col1, collect_set(col2) from table1 group by col1
SELECT
userid,
concat_ws(" ", collect_set(col)) AS combined
FROM table
GROUP BY userid