hive--如何保证concat_ws内部有序

  • 大概思路
    • row_number()先排序,得到序号
    • 把序号和字段拼接concat_ws
    • sort_array
    • 再concat_ws
    • 把数字替换掉
select  m.city_id                                                                                                                  as city_code
       ,m.class_id                                                                                                                 as ct_class_id  
       ,regexp_replace(concat_ws(',',sort_array(collect_set(concat_ws(':',cast(row_id as string),tea_id)))),'\\d+\:','')           as tea_ids
       ,regexp_replace(concat_ws(',',sort_array(collect_set(concat_ws(':',cast(row_id as string),tea_teacher_name)))),'\\d+\:','') as tea_teacher_names
from
(
    select  tct.city_id
           ,tct.class_id  
           ,tet.tea_id
           ,tet.tea_teacher_name
           ,tet.tea_emp_no
           ,tet.tea_teacher_code
           ,row_number() over (partition by tct.city_id,tct.class_id order by nvl(tet.tea_emp_no,tet.tea_teacher_code)*1)  as row_id
    from class_teacher tct
    inner join teacher tet
        on tct.city_id=tet.city_id 
        and tct.teacher_id=tet.tea_id
) m
group by  m.city_id
         ,m.class_id 
;
  • 有朋友问为什么*1
    • 随手写了个临时表,大家一眼就能看出来为什么了~
    • 这里的序号生成方式要结合业务具体分析~

drop table if exists bi_temp.hzy_20220825;
create table bi_temp.hzy_20220825 as 
select '1' as a 
union all
select '2' as a
union all
select '3' as a
union all
select '11' as a
union all
select '22' as a
union all
select '33' as a
;

select *
	,dense_rank() over (order by a) as rank
from  bi_temp.hzy_20220825 
;
a	rank	
1	1
11	2
2	3
22	4
3	5
33	6


select *
	,dense_rank() over (order by a*1) as rank
from  bi_temp.hzy_20220825 
;
a	rank	
1	1
2	2
3	3
11	4
22	5
33	6
  • 有朋友说要 用 lapd 左补足
    • 具体还是要看你的业务场景,左补足也行
      • lpad(row_number() over (partition by tct.city_id,tct.class_id order by nvl(tet.tea_emp_no,tet.tea_teacher_code)*1) ,10,‘0’) as row_id
    • 函数不熟悉的,可以参考这篇hive函数
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值