Concat使用GROUP BY的单列字段 [英] Concat single column fields using GROUP BY

查看:137
本文介绍了Concat使用GROUP BY的单列字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以通过对一列中的字段进行分组来组合/连接字段。
例如:

Is there any way to combine/concat the fields within one column by grouping them. Eg:

col1   col2
1     aa
1     bb
1     cc
2     dd
2     ee

我想查询如下内容:

select col1, concat(col2) from tableName group by col1;

输出应该是:

Output should be :

1    aa,bb,cc
2    dd,ee

在配置单元中是否有这样的功能?

Is there any function in hive to do this ?

推荐答案

假设你有一个表 test ,如下所示:

Suppose you have a table test as follows:

select id, val from test order by id, val;     
2   aa
2   bb
1   bb
1   aa

您可以使用 HIVE 函数 collect_set

You can use the HIVE function collect_set:

select id, collect_set(val) from test group by id;
1   ["aa","bb"]
2   ["bb","aa"]

但请注意, collect_set 会返回一组消除重复元素的对象。

But note that collect_set returns a set of objects with duplicate elements eliminated.

您可以在语言手册Wiki 中找到更多详细信息。

You can find more details at the Language Manual Wiki.

这篇关于Concat使用GROUP BY的单列字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆