拆分一列为多行的问题

[复制链接]
查看11 | 回复8 | 2007-1-6 21:14:12 | 显示全部楼层 |阅读模式
表数据如下
id,name,code
1,abc,c001/c002/c007
2,efg,c001/c003
3,ttt,c008/c010
怎么能转换成
id,name,code
1,abc,c001
1,abc,c002
1,abc,c007
2,efg,c001
2,efg,c003
3,ttt,c008
3,ttt,c010
谢谢
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
用存储过程做一下,好了,简简单单
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
老大,怎么写呀
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
试试这个
[php]
with tt as (
select 1 id ,'abc' name ,'c001/c002/c007' code from dual union all
select 2,'efg','c001/c003' from dual union all
select 3,'ttt','c008/c010' from dual
)
select * from (
select id,
name,
rn,
decode(rn,

1,

substr(code, 1, instr('c001/c002/c007', '/', 1, 1)-1),

2,

substr(code, instr('c001/c002/c007', '/', 1, 1)+1,

instr('c001/c002/c007', '/', 1, 2)-instr('c001/c002/c007', '/', 1, 1)-1),

3,

substr(code,instr('c001/c002/c007', '/', 1, 2)+1)

) rnn
from (select * from tt, (select rownum rn from dual connect by rownumcreate table h_t(id number ,name varchar2(20),code varchar2(50));
表已创建。
SQL> begin
2insert into h_t values (1, 'abc', 'c001/c002/c007');
3insert into h_t values (2, 'efg', 'c001/c003');
4insert into h_t values (3, 'ttt', 'c008/c010');
5commit;
6end;
7/
PL/SQL 过程已成功完成。
SQL> select * from h_t;
ID NAME
CODE
---------- -------------------- --------------------------------------------------
1 abc
c001/c002/c007
2 efg
c001/c003
3 ttt
c008/c010
SQL> create table h_t_1 as select * from h_t where rownumdeclare
2l_i number;
3l_p number;
4l_times number;
5l_str varchar2(10);
6begin
7for x in (select * from h_t) loop
8l_i := 1;
9l_times := length(x.code) - length(replace(x.code, '/', '')) + 1;
10for y in 1 .. l_times loop
11l_p := l_i;
12l_i := instr(x.code || '/', '/', l_i) + 1;
13l_str := substr(x.code, l_p, l_i - l_p - 1);
14insert into h_t_1 values (x.id, x.name, l_str);
15end loop;
16end loop;
17commit;
18end;
19/
PL/SQL 过程已成功完成。
SQL> select * from h_t_1;
ID NAME
CODE
---------- -------------------- --------------------------------------------------
1 abc
c001
1 abc
c002
1 abc
c007
2 efg
c001
2 efg
c003
3 ttt
c008
3 ttt
c010
已选择7行。
[/php]
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
最初由 nokilled 发布
[B][php]
SQL> create table h_t(id number ,name varchar2(20),code varchar2(50));
表已创建。
SQL> begin
2insert into h_t values (1, 'abc', 'c001/c002/c007');
3insert into h_t values (2, 'efg', 'c001/c003');
4insert into h_t values (3, 'ttt', 'c008/c010');
5commit;
6end;
7/
PL/SQL 过程已成功完成。
SQL> select * from h_t;
ID NAME
CODE
---------- -------------------- --------------------------------------------------
1 abc
c001/c002/c007
2 efg
c001/c003
3 ttt
c008/c010
SQL> create table h_t_1 as select * from h_t where rownumdeclare
2l_i number;
3l_p number;
4l_times number;
5l_str varchar2(10);
6begin
7for x in (select * from h_t) loop
8l_i := 1;
9l_times := length(x.code) - length(replace(x.code, '/', '')) + 1;
10for y in 1 .. l_times loop
11l_p := l_i;
12l_i := instr(x.code || '/', '/', l_i) + 1;
13l_str := substr(x.code, l_p, l_i - l_p - 1);
14insert into h_t_1 values (x.id, x.name, l_str);
15end loop;
16end loop;
17commit;
18end;
19/
PL/SQL 过程已成功完成。
SQL> select * from h_t_1;
ID NAME
CODE
---------- -------------------- --------------------------------------------------
1 abc
c001
1 abc
c002
1 abc
c007
2 efg
c001
2 efg
c003
3 ttt
c008
3 ttt
c010
已选择7行。
[/php] [/B]

干净利索,佩服
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
強悍,學習了。
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
从nokilled的变量命名上就知道是个高手了,学习!
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
会不难,写好就难了.
回复

使用道具 举报

千问 | 2007-1-6 21:14:12 | 显示全部楼层
非常好的东东!谢谢啦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行