首页 > 数据库技术 > 详细

Oracle中的列转行例子详解

时间:2017-11-04 14:34:48      阅读:305      评论:0      收藏:0      [点我收藏+]

数据如下:
name id
张三 1,2,3

要求实现:
name id
张三 1
张三 2
张三 3

--创建临时表
create table tmp as(select 张三 name, 1,2,3 id from dual);
--写法1
select name,regexp_substr(id,[^,]+,1,level) as id
from tmp
connect by level <= regexp_count(id,,)+1
order by id

--写法2:
select name,trim(regexp_substr(id,[^,]+,1,level)) as id
from tmp
connect by name = prior name 
and prior dbms_random.value is not null
and level <= length(regexp_replace(id, [^,]))+1;

--写法3
select name,
--regexp_replace(id,‘(\w+)\,(\w+)\,(\w+)‘,level) id
regexp_replace(id,(\w+)\,(\w+)\,(\w+),\||to_char(level)) id
from tmp
connect by level <= regexp_count(id,,)+1;

--写法4: 
select name,
substr(,||id||,,instr(,||id||,, ,, 1, level) + 1, instr(,||id||,, ,, 1, level + 1) -( instr(,||id||,, ,, 1, level) + 1))
from tmp
connect by level <= regexp_count(id,,)+1
order by level

此外,列转行还可以使用union all和unpivot(oracle 11g新特性)等,待后续补充

Oracle中的列转行例子详解

原文:http://www.cnblogs.com/huangbiquan/p/7783080.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!