hive sql 行转列 collect_set,collect_list 有序化展示

记录自己平时处理的笔记,使用前还是自己验证一下吧

1、collect_set去除重复元素;collect_list不去除重复元素;需要进行group by
select phone,collect_list(user_id) ,collect_set(user_id)
from a
group by phone
123456789 [1,3,2,1,2] [1,3,2]

2、collect_list 展示子表排序后结果,collect_set 不受子表排序影响
select phone,collect_list(user_id) ,collect_set(user_id) from
(select * from a order by order_time asc)b
group by phone
结果:123456789 [1,1,3,2,2] [1,3,2]

a表数据如下
phone user_id order_time
123456789 1 2018/8/23
123456789 3 2018/8/24
123456789 2 2018/8/25
123456789 1 2018/8/22
123456789 2 2018/8/26

你可能感兴趣的:(sql)