码迷,mamicode.com
首页 > 数据库 > 详细

Oracle中的列转行例子详解

时间:2017-11-04 15:06:42      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:nio   char   列转行   logs   order   weight   random   tab   level   

数据如下:
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中的列转行例子详解

标签:nio   char   列转行   logs   order   weight   random   tab   level   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!