HIVE 语法

3. concat_ws(seperator, string s1, string s2...)
功能:制定分隔符将多个字符串连接起来,实现“列转行”
例子:常常结合group by与collect_set使用

有表结构a string , b string , c int
数据为
c d 1
c d 2
c d 3
e f 4
e f 5
e f 6

想要得到


c d 1,2,3
e f 4,5,6

语句:
select a, b, concat_ws(',' , collect_set(cast(c as string)))
from table group by a,b;


上述用的到的 collect_set 函数,有两个作用,第一个是去重,去除group by后的重复元素,
第二个是形成一个集合,将group by后属于同一组的第三列集合起来成为一个集合。与contact_ws
结合使用就是将这些元素以逗号分隔形成字符串。

posted @ 2021-03-11 15:47  王晓天  阅读(51)  评论(0编辑  收藏  举报