使用pl/sql develop 编写一个行列变换的sql语句
来源:百度知道 编辑:UC知道 时间:2024/05/31 06:29:50
姓名 课程 分数
张三 数学 100
张三 英语 90
张三 语文 90
李四 数学 80
... ... ...
通过上表,取数变成
姓名,课程1,分数1,课程2,分数2,课程3,分数3
张三 数学 100 英语 90 语文 90
同时,存在李四只有数学课,王五只学了语文课。
需要使用oracle数据库统计。
可以使用decode
行列转换很复杂,给你做参考
转换前:
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