hive collect_set mysql group_concat

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.hive中collect_set用法

hive表有两列,其中一列为id,另外一列为channel。现在想把相同id的channel聚合到一块并去重。
比如表中如下数据
id1 c1
id2 c2
id1 c2
id1 c1
id1 c3
id1 c3
id2 c4
输出结果如下
id1 c1,c2,c3
id2 c2,c4

用collect_set函数可以满足上面的要求

select id, concat_ws(',', collect_set(channel)) from tablexxx group by id

如果channel不要求去重,将collect_set改为collect_list即可。

2.mysql中group_concat

在mysql中也有类似的函数group_concat
假设mysql表同样有两列:id,name。现在将相同id的name聚合在一起,假设表中数据为
id1 a
id1 b
id1 c
id1 a
id2 e
id2 f

默认的分隔符是","

select id,group_concat(name separator ';') from tablexxx group by id;

如果要对name去重

select id,group_concat(distinct name) from tablexxx group by id;

如果还想对name排序

select id,group_concat(name order by name desc) from tablexxx group by id;  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值