【发布时间】:2011-04-11 20:24:42
【问题描述】:
所以我有 5 行这样的
userid, col
--------------
1, a
1, b
2, c
2, d
3, e
我将如何进行查询,使其看起来像这样
userid, combined
1, a b
2, c d
3, e
【问题讨论】:
所以我有 5 行这样的
userid, col
--------------
1, a
1, b
2, c
2, d
3, e
我将如何进行查询,使其看起来像这样
userid, combined
1, a b
2, c d
3, e
【问题讨论】:
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
【讨论】:
在蜂巢中你可以使用
SELECT userid, collect_set(combined) FROM tabel GROUP BY user_id;
collect_set 删除重复的。如果您需要保留它们,可以查看此帖子:
【讨论】:
使用GROUP_CONCAT aggregate function:
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
【讨论】: