+ -
当前位置:首页 → 问答吧 → 一行变多行

一行变多行

时间:2011-10-10

来源:互联网

有一张表A格式为

id code price
1 100,101, 50
2 102,103, 20
3 104, 30

我现在想要
id code price
1 100 50
1 101 50
2 102 20
2 103 20
3 104 30

谢谢!

作者: lwl0376   发布时间: 2011-10-10

列转行。

作者: lzd_83   发布时间: 2011-10-10

楼上的,啥意思啊?请在明示,谢谢

作者: lwl0376   发布时间: 2011-10-10

引用 2 楼 lwl0376 的回复:
楼上的,啥意思啊?请在明示,谢谢

楼上的意思,就是:
列转行:
列:code列,以逗号分隔有两个部分, 要转成两行,每行的code值都是逗号间的值。

作者: yixilan   发布时间: 2011-10-10

SQL code

with t as (
select 1 id, '100,101,' code, 50 price from dual union
select 2, '102,103,', 20 from dual union
select 3, '104,', 30 from dual 
)
select t.id,rtrim(regexp_substr(code,'[0-9]+,',1,rn),',') code2
from t,(select rownum rn from dual connect by level < 100) r
where regexp_substr(code,'[0-9]+,',1,rn) is not null
order by t.id,code2
-- 其中level < 100这里的100是取你code字段的最大分割次数,如果超出100你可以把这个值上调。

作者: xiaobn_cn   发布时间: 2011-10-10