兩種簡單的行列轉置:
1、固定列數的行列轉換
如student subject grade
--------- ---------- --------
student1 語文 80
student1 數學 70
student1 英語 60
student2 語文 90
student2 數學 80
student2 英語 100
……轉換為
語文 數學 英語
student1 80 70 60
student2 90 80 100
語句如下:
select student,
sum(decode(subject,'語文', grade,null)) "語文",
sum(decode(subject,'數學', grade,null)) "數學",
sum(decode(subject,'英語', grade,null)) "英語"
from table
group by student;
2、不定列行列轉換
如c1 c2
--- -----------
1 我1 是
1 誰2 知
2 道3 不
……轉換為
1 我是誰
2 知道
3 不這一型別的轉換必須借助於pl/sql來完成,這裡給乙個例子
create or replace function get_c2(tmp_c1 number)
return varchar2
iscol_c2 varchar2(4000);
begin
for cur in (select c2 from t where c1=tmp_c1) loop
col_c2 := col_c2||cur.c2;
end loop;
col_c2 := rtrim(col_c2,1);
return col_c2;
end;
/sql> select distinct c1 ,get_c2(c1) cc2 from table;
如何用sql實現乙個結果集的行列轉置?
以此為準:
with tt as (select 1 a , 2 b , 3 c from dual
union all
select 2 a , 2 b , 4 c from dual
union all
select 4 a , 3 b , 3 c from dual
union all
select 3 a , 1 b , 6 c from dual
union all
select 4 a , 3 b , 1 c from dual
union all
select 6 a , 5 b , 9 c from dual)
謝謝!!
能說明白一點嗎?
select 1 a , 2 b , 3 c from dual
union all
select 2 a , 2 b , 4 c from dual
union all
select 4 a , 3 b , 3 c from dual
union all
select 3 a , 1 b , 6 c from dual
union all
select 4 a , 3 b , 1 c from dual
union all
select 6 a , 5 b , 9 c from dual
結果為:
a b c
1 2 3
2 2 4
4 3 3
3 1 6
4 3 1
6 5 9
給這個變成什麼樣?
可以這樣!!!!!1
先建立個表
create table twb06
(a varchar2(10),
b varchar2(10),
c varchar2(20)
)插入資料
a b c
1 2 3
2 2 4
4 3 3
3 1 6
4 3 1
6 5 9
然後這樣,就行了
select * from (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 twb06) )
where rownum=1 union
select * from (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 twb06) )
where rownum=1
union
select * from (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 twb06) )
where rownum=1
剛才寫多了,這樣就行
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 twb06)
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 twb06)
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 twb06)
where rownum=1
Oracle 行列轉置
兩種簡單的行列轉置 1 固定列數的行列轉換 如student subject grade student1 語文 80 student1 數學 70 student1 英語 60 student2 語文 90 student2 數學 80 student2 英語 100 轉換為 語文 數學 英語 s...
行列轉置(Oracle)
行列轉換包含如下幾種形式 行轉列 列轉行 多列轉換成字串 多行轉換成字串 字串轉換成多列 字串轉換成多行 create table student id number name varchar2 20 course varchar2 20 score number 插入測試資料,如下 結果集如下 s...
SQL 行列轉置
我學會了第二種方法 sql2005中的方法 create table tb id int,value varchar 10 insert into tb values 1,aa insert into tb values 1,bb insert into tb values 2,aaa insert...