Hive collect_set()、collect_list()列转行,和concat_ws()使用,并对转换后的行值排序
对列值分组,并按一定顺序排序,最后多行合并一行,合并值左到右逆序排列。
select st_name
,concat_ws(",",sort_array(collect_list(class),false))
,concat_ws(",",sort_array(collect_list(class),true))
,concat_ws(",",sort_array(collect_list(class)))
from
(
select "jack" as st_name, '3' as class
union all
select "jack" as st_name, '1' as class
union all
select "jack" as st_name, '2' as class
union all
select "jack" as st_name, '3' as class
union all
select "jack" as st_name, '5' as class
)tb_mid
group by st_name;
结果如下:
st_name concat_ws(,, sort_array(collect_list(class), false)) concat_ws(,, sort_array(collect_list(class), true)) concat_ws(,, sort_array(collect_list(class), true))
jack 5,3,3,2,1 1,2,3,3,5 1,2,3,3,5
Time taken: 0.16 seconds, Fetched 1 row(s)
select st_name
,concat_ws(",",sort_array(collect_set(class),false))
,concat_ws(",",sort_array(collect_set(class),true))
,concat_ws(",",sort_array(collect_set(class)))
from
(
select "jack" as st_name, '3' as class
union all
select "jack" as st_name, '1' as class
union all
select "jack" as st_name, '2' as class
union all
select "jack" as st_name, '3' as class
union all
select "jack" as st_name, '5' as class
)tb_mid
group by st_name;
结果如下:
st_name concat_ws(,, sort_array(collect_set(class), false)) concat_ws(,, sort_array(collect_set(class), true)) concat_ws(,, sort_array(collect_set(class), true))
jack 5,3,2,1 1,2,3,5 1,2,3,5
Time taken: 0.152 seconds, Fetched 1 row(s)