2013年(350)
分类: Oracle
2013-04-24 13:27:31
呵呵,又来一个行转列,难道我跟行转列卯上了?听说11g中有了更简单的法子来实现行列转换,黑黑,说不得以后还得再来一篇啊。
create table tmp5 (id number(2),name varchar2(10),code varchar2(50));
alter table tmp5 add constraint pk_tmp5 primary key (id);
insert into tmp5 values (1,'aaa','c001/c002/c007');
insert into tmp5 values (2,'bbb','c001/c003');
insert into tmp5 values (3,'ccc','c008/c0011/c029/c023');
insert into tmp5 values (4,'ddd','c102/c111/c112/c144/c167');
select id,
name,
--rn,
--code,
substr(code, st, decode(ed, 0, length(code), ed - st)) code
from (select id,
name,
code,
rn,
decode(rn, 1, 1, instr(code, '/', 1, rn - 1) + 1) st,
instr(code, '/', 1, rn) ed
from (select *
from tmp5,
(select rownum rn
from dual
connect by rownum <=
(select max(length(code)) -
max(length(replace(code, '/', ''))) + 1
from tmp5))))
where ed > 0
or st > 1
order by id, rn;