oracle 拆分类型为多行(regexp_substr) 与 多个行汇总合并(listagg(codecname,‘,‘)within group(order by endortype))到一个字段

–regexp_count(br_level, ‘.’) 统计字符.的个数
–regexp_substr(br_level, ‘[^.]+’, 1, level) br_level字段从位置1开始使用字符.拆分,level指取拆分后第level个字符串

–拆分批单批改类型 为多行
create table tmp_car_endor as
select * from (
with tmp_car as
(
select t.endorseno,t.endortype from prpphead t where t.endorseno in (select t2.endorseno from tmp_car_all t2)
),
endortype_level as(
select level lv from dual connect by level < 10
)
select t.endorseno,
a.lv,
regexp_count(t.endortype, ‘,’) + 1 as row_cnt,
regexp_substr(t.endortype, ‘[^,]+’, 1, a.lv) as endortype
from tmp_car t
inner join endortype_level a
on a.lv <= regexp_count(t.endortype, ‘,’) + 1
) mm order by mm.endortype
;

–将一个批单的多个批改类型名称汇总合并到一个字段
create table tmp_car_endortype_name as
select t.endorseno,listagg(codecname,‘,’)within group(order by endortype) as endortype_name
from tmp_car_endor t
left join prpdcode t2 on t.endortype = t2.codecode
where t2.codetype=‘EndorType’
group by t.endorseno
;

引用别人的方法:
–2. 多条数据拆分
–1、方法1
–正确
with z_branch as(
select ‘BR1001’ brid, ‘BR1001.BR1002.BR1003’ br_level from dual
union all
select ‘BR1002’ brid, ‘BR1002.BR1003’ br_level from dual
union all
select ‘BR1003’ brid, ‘BR1001.BR1003’ br_level from dual
)
select brid,
level,
regexp_count(br_level, ‘.’) + 1 as row_cnt,
regexp_substr(br_level, ‘[^.]+’, 1, level) as s_brid
from z_branch t
connect by level <= regexp_count(br_level, ‘.’) + 1
and t.brid = prior t.brid
and prior dbms_random.value > 0
;

–2、方法2
–正确 借助伪列
with z_branch as(
select ‘BR1001’ brid, ‘BR1001.BR1002.BR1003’ br_level from dual
union all
select ‘BR1002’ brid, ‘BR1002.BR1003’ br_level from dual
union all
select ‘BR1003’ brid, ‘BR1001.BR1003’ br_level from dual
),
z_level as(
select level lv from dual connect by level < 10
)
select t.brid,
a.lv,
regexp_count(t.br_level, ‘.’) + 1 as row_cnt,
regexp_substr(t.br_level, ‘[^.]+’, 1, a.lv) as s_brid
from z_branch t
inner join z_level a
on a.lv <= regexp_count(t.br_level, ‘.’) + 1;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值