一行变多行

lwl0376 2011-10-10 09:21:17
有一张表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

谢谢!
...全文
150 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BenChiM888 2011-10-10
  • 打赏
  • 举报
回复


--手上没有oracle环境,随便写了下。
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,regexp_substr(code,'[^,]+',1,l)
from t,(select level l from dual connect by level < 10) r --这里的10表示最多有10-1=9个逗号分隔。如果不过自行改正。
where length(code)-length(replace(code,',',''))+1>r.l
order by t.id,1;
xiaobn_cn 2011-10-10
  • 打赏
  • 举报
回复

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你可以把这个值上调。
yixilan 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lwl0376 的回复:]
楼上的,啥意思啊?请在明示,谢谢
[/Quote]
楼上的意思,就是:
列转行:
列:code列,以逗号分隔有两个部分, 要转成两行,每行的code值都是逗号间的值。
lwl0376 2011-10-10
  • 打赏
  • 举报
回复
楼上的,啥意思啊?请在明示,谢谢
Rotel-刘志东 2011-10-10
  • 打赏
  • 举报
回复
列转行。
yixilan 2011-10-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 benchim888 的回复:]
SQL code


--手上没有oracle环境,随便写了下。
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,rege……
[/Quote]
太厉害了,没有环境随便写的SQL也没错误。。。。

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧