魏海飞 2023-04-17 10:30 采纳率: 50%
浏览 44
已结题

oracle把一行拆分为多行

已有课表数据,需要把ZC拆分为多行数据,其他列数据不变,如下图:

img

以下为拆分后的效果

img

  • 写回答

2条回答 默认 最新

  • mzs1101 2023-04-17 17:04
    关注
    
    select * from (select ag.startnum, ag.endnum,ag.zs,ag.startnum+levels.maxlev-1 as newzs, ag.xqj, ag.skdd, ag.js, ag.xq, ag.ld
      from (select substr(zs, 0, instr(zs, '-') - 1) startnum,
                   substr(zs, instr(zs, '-') + 1, length(zs)) endnum,
                   substr(zs, instr(zs, '-') + 1, length(zs)) -
                   substr(zs, 0, instr(zs, '-') - 1) + 1 as lev,
                   zs,
                   xqj,
                   skdd,
                   js,
                   xq,
                   ld
              from mzs_test) ag,/*找出需要循环的数据*/
           (select rownum maxlev
              from (select max(substr(zs, instr(zs, '-') + 1, length(zs)) -
                               substr(zs, 0, instr(zs, '-') - 1) + 1) maxl
                      from mzs_test) agg
            connect by level <= agg.maxl) levels /*找出需要循环的层数*/
     where levels.maxlev <= ag.lev
     order by zs
     ) rs order by rs.zs,rs.newzs asc
    

    =================原始数据表===================================

    img

    ================查询结果表===============================================

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月26日
  • 已采纳回答 4月18日
  • 创建了问题 4月17日