Hive | 用sort_array函数解决collet_list列表排序混乱问题

由collect_list形成的列表经过concat_ws拼接后顺序具有随机性,要保证列表有序只需要在生成列表后使用sort_array函数进行排序即可,示例如下:

SELECT 
    memberid,
    regexp_replace(
        concat_ws('-',
                    sort_array(
                                collect_list(
                                            concat_ws(':',cast(legcount as string),airways)
                                            )
                                )
                    ),'\\d\:','') hs
from 
(
select 1 as memberid,'A' as airways,2 as legcount
union ALL
select 1 as memberid,'B' as airways,3 as legcount
union ALL
select 1 as memberid,'C' as airways,4 as legcount
union ALL
select 1 as memberid,'D' as airways,1 as legcount
union ALL
select 1 as memberid,'E' as airways,8 as legcount
) as t
group by memberid

构造数据(memberid为会员ID,airway为会员预定机票选择的航司,legcount为下单航段)

memberid airways legcount
1 A 2
1 B 3
1 C 4
1 D 1
1 E 8

运行结果:

memberid hs
1 D-A-B-C-E

sort_array貌似不能降序排列,如果要倒序排的话在子查询里新增一个辅助列来排序即可。

你可能感兴趣的:(Hive)