oracle 行列转换

oracle 行列转换 (http://blog.163.com/xiao_maomao_chong/blog/static/3168623920086445319803/)

笔 试 2008-07-04 16:53:19 阅读118 评论0 字号:

有一个字符串

,1,2,5,9,1,2,5,9,1,3,9,

通 过SQL文将以上字符串处理后得到如下查询结果:

        col
        1
        2
        5
        9
        1
        2
        5
        9
        1
        3
        9


with tbl2 as (
select ',1,2,5,9,1,2,5,9,1,3,9,22,25,10,155555,'  as temp from dual
)
select substr(temp,currentIndex,nextIndex-currentIndex) as cols  from
(
    select temp
          ,level lv
          ,instr(temp,',',instr(temp,','),level)+1 as currentIndex
          ,instr(temp,',',instr(temp,','),level+1) as nextIndex from 
        (
        select temp
              ,length(temp)- length(replace(temp,','))-1 rowcnt
        from tbl2
        ) a
    connect by level <= rowcnt
)

挑战无处不在~~


with tbl2 as (
select ',1,2,5,9,1,2,5,9,1,3,9,'  as temp from dual
)
select substr(temp2,lv,1) from
(
    select temp2,level lv from 
        (
        select temp,replace(temp,',') temp2,length(replace(temp,',')) rowcnt from tbl2
        ) a
    connect by level <= rowcnt
)


select DECODE('A', '01' ,'WO JIAO DDDD', 'EEEE' , '02', '55' )
AS LIU from
(
select
 SUBSTR(mid,1,2) as a,
 SUBSTR(mid,4,2) as b,
 SUBSTR(mid,7,2) as c,
SUBSTR(mid,10,2) as d,
SUBSTR(mid,13,2) as e
 from liu )

**********************************************************************************

select DECODE(a, '01' ,b, '02' , c, d )
AS LIU from
(
select
 SUBSTR(mid,1,2) as a,
 SUBSTR(mid,4,2) as b,
 SUBSTR(mid,7,2) as c,
SUBSTR(mid,10,2) as d,
SUBSTR(mid,13,2) as e
 from liu )

 

 

with tbl2 as (
select wmsys.wm_concat(mid) as temp from liu
)
select substr(temp,currentIndex,nextIndex-currentIndex) as cols  from
(
    select temp
          ,level lv
          ,instr(temp,',',instr(temp,','),level)-2 as currentIndex
          ,instr(temp,',',instr(temp,','),level) as nextIndex from 
        (
        select temp
             ,length(replace(temp,','))/2 rowcnt
        from tbl2
        ) a
    connect by level <= rowcnt
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值