无分隔符的字段分组拆分

前几天解答的题目

把下面例子中fq字段按每个字符来分组拆分

原始数据

SEASONMONTHCODENOFQDAYS
17S_A4AB800112345677
18S_A4CD80022463
19S_A5EF8003152

要实现的结果

SEASON    MONTH    CODE    NO    FQ
17S_A    4    AB    8001    1
17S_A    4    AB    8001    2
17S_A    4    AB    8001    3
17S_A    4    AB    8001    4
17S_A    4    AB    8001    5
17S_A    4    AB    8001    6
17S_A    4    AB    8001    7
18S_A    4    CD    8002    2
18S_A    4    CD    8002    4
18S_A    4    CD    8002    6
19S_A    5    EF    8003    1
19S_A    5    EF    8003    5

SQL:

  1. with d as
  2.  (select '17S_A' as season,
  3.          4 as month,
  4.          'AB' as code,
  5.          8001 as no,
  6.          1234567 as fq,
  7.          7 as days
  8.     from dual
  9.   union all
  10.   select '18S_A' as season,
  11.          4 as month,
  12.          'CD' as code,
  13.          8002 as no,
  14.          246 as fq,
  15.          3 as days
  16.     from dual
  17.   union all
  18.   select '19S_A', 5, 'EF', 8003, 15, 2 from dual)
  19. select season, month, code, no, substr(fq, level, 1) fq
  20.   from d
  21. connect by level <= length(fq)
  22.        and season = prior season
  23.        and month = prior month
  24.        and code = prior code
  25.        and no = prior no
  26.        and prior dbms_random.value is not null;

下面是按照两个字节拆分的例子

  1. with d as
  2.  (select '17S_A' as season,
  3.          4 as month,
  4.          'AB' as code,
  5.          8001 as no,
  6.          1234567 as fq,
  7.          7 as days
  8.     from dual
  9.   union all
  10.   select '18S_A' as season,
  11.          4 as month,
  12.          'CD' as code,
  13.          8002 as no,
  14.          246 as fq,
  15.          3 as days
  16.     from dual
  17.   union all
  18.   select '19S_A', 5, 'EF', 8003, 15, 2 from dual)
  19. select season, month, code, no, substr(fq, 2 * (level - 1) + 1, 2) fq
  20.   from d
  21. connect by level < length(fq) / 2 + 1
  22.        and season = prior season
  23.        and month = prior month
  24.        and code = prior code
  25.        and no = prior no
  26.        and prior dbms_random.value is not null;

结果:

SEASONMONTHCODENOFQ
17S_A4AB800112
17S_A4AB800134
17S_A4AB800156
17S_A4AB80017
18S_A4CD800224
18S_A4CD80026
19S_A5EF800315
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值