兩種簡單的行列轉置
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
bystudent;
2、不定列行列轉換
如c1 c2
--- -----------
1 我
1 是
1 誰
2 知
2 道
3 不
……轉換為
1 我是誰
2 知道
3 不這一型別的轉換必須借助於pl/sql來完成,這裡給乙個例子
create
orreplace
function
get_c2(tmp_c1
number
)return
varchar2
iscol_c2
varchar2
(4000
);begin
forcur in(
select
c2 from
t wherec1=
tmp_c1) loop
col_c2 :
=col_c2
||cur.c2;
endloop;
col_c2 :
=rtrim
(col_c2,1);
return
col_c2;
end;
/sql
>
select
distinct
c1 ,get_c2(c1) cc2
from
table;
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...