转换前:
select * from test;
A B C
-- -- --
1 2 3
2 2 4
4 3 3
3 1 6
4 3 6
6 5 9
8 8 8
转换后:
A B C D E
------------ -------------- --------------- ------------- -------------
1 2 4 3 4
2 2 3 1 3
3 4 3 6 6
方法:(有三种)
--牛逼的方法(悟其精髓确有难度)
select regexp_substr(str, '[^,]+', 1, 1) a,
regexp_substr(str, '[^,]+', 1, 2) b,
regexp_substr(str, '[^,]+', 1, 3) c,
regexp_substr(str, '[^,]+', 1, 4) d,
regexp_substr(str, '[^,]+', 1, 5) e
from (select regexp_substr(str, '[^(\.)]+', 1, rownum) str
from (select max(a) || '.' || max(b) || '.' || max(c) str
from (select substr(sys_connect_by_path(a, ','), 2) a,
substr(sys_connect_by_path(b, ','), 2) b,
substr(sys_connect_by_path(c, ','), 2) c
from (select rownum child,
a,
b,
c,
lead(rownum, 1) over(order by rownum) parent
from test) t
start with child = 1
connect by prior parent = child))
connect by rownum < length(regexp_replace(str, '[^(\.)]', '')) + 2);
--正常的方法(大家都想的到)
select *
from (select a a1,
lead(a, 1) over(order by rownum) a2,
lead(a, 2) over(order by rownum) a3,
lead(a, 3) over(order by rownum) a4,
lead(a, 4) over(order by rownum) a5,
lead(a, 5) over(order by rownum) a6
from test)
where rownum = 1
union
select *
from (select b a1,
lead(b, 1) over(order by rownum) a2,
lead(b, 2) over(order by rownum) a3,
lead(b, 3) over(order by rownum) a4,
lead(b, 4) over(order by rownum) a5,
lead(b, 5) over(order by rownum) a6
from test)
where rownum = 1
union
select *
from (select c a1,
lead(c, 1) over(order by rownum) a2,
lead(c, 2) over(order by rownum) a3,
lead(c, 3) over(order by rownum) a4,
lead(c, 4) over(order by rownum) a5,
lead(c, 5) over(order by rownum) a6
from test)
where rownum = 1;
--方法太多了(你对oracle熟悉么?)
select max(decode(rn, 1, a, null)) id1,
max(decode(rn, 2, a, null)) id2,
max(decode(rn, 3, a, null)) id3,
max(decode(rn, 4, a, null)) id4,
max(decode(rn, 5, a, null)) id5,
max(decode(rn, 6, a, null)) id6
from (select a.*, rownum rn from test a)
union
select max(decode(rn, 1, b, null)) id1,
max(decode(rn, 2, b, null)) id2,
max(decode(rn, 3, b, null)) id3,
max(decode(rn, 4, b, null)) id4,
max(decode(rn, 5, b, null)) id5,
max(decode(rn, 6, b, null)) id6
from (select a.*, rownum rn from test a)
union
select max(decode(rn, 1, c, null)) id1,
max(decode(rn, 2, c, null)) id2,
max(decode(rn, 3, c, null)) id3,
max(decode(rn, 4, c, null)) id4,
max(decode(rn, 5, c, null)) id5,
max(decode(rn, 6, c, null)) id6
from (select a.*, rownum rn from test a);
end p_test_row;
相关推荐
本例子主要是实现了oracle行列的转换,这个挺有用的
Oracle行列转换,一个挺经典的例子,值得学习和思考……
oracle行列转换,通过实例来实现表的行列转换
oracle行列转换
oracle行. 列转换的总结。。。。。。。。。。。。
ORACLE 行列转换,ORACLE,行列转换
行列转换,sys_connect_by_path,row_number等函数的用法
oracle行列转换示例,查询转换,有创表过程,数据,清晰明了
Oracle 行列转换 总结 很实用的一个文档。
oracle的行列转换例子,行转列,列转行。主要针对oracle10g
常见的数据库面试关于行转列列转行的操作,方便你的面试,经常考到的
本人在工作中遇到了涉及到数据库行列之间相互转换的问题,在网上搜索了很久也没有一个比较完整的解释,通过本人自己的摸索测试,整理出来了Oracle中SQL语句行列之间相互转换的资料,大家可以下载学习。
oracle行列转换[文].pdf
行列转换包括以下六种情况: 1. 列转行 2. 行转列 3. 多列转换成字符串 4. 多行转换成字符串 5. 字符串转换成多列 6. 字符串转换成多行
详细的例子描述各类行列转换,非常好的一份pdf文档, 强烈推荐
很通俗易懂的行列转换的例子,适用于oracle数据库,我一看都明白了
oracle知识列点 一个简单的行列转换例子,需要的可以看下