列轉行 行轉列

2021-06-07 09:48:52 字數 3432 閱讀 1022

問題:使用case when實現行轉列

解決:1、測試準備:

create  table studentscores(

username nvarchar2(20), --學生姓名

subject   nvarchar2(30), --科目

score     float         --成績);

2、準備資料:

insert into studentscores values ('nick', '

語文', 80);

insert into studentscores values ('nick', '數學', 90);

insert into studentscores values ('nick', '英語', 70);

insert into studentscores values ('nick', '生物', 85);

insert into studentscores values ('kent', '語文', 80);

insert into studentscores values ('kent', '數學', 90);

insert into studentscores values ('kent', '英語', 70);

insert into studentscores values ('kent', '生物', 85);

commit;

目前的資料形式為:

目標的資料形式:

3、實現方式:

select

username,

max(case when subject='語文' then score else 0 end) as "語文",

max(case when subject='數學' then score else 0 end) as "數學",

max(case when subject='英語' then score else 0 end) as "英語",

max(case when subject='生物' then score else 0 end) as "生物"

from studentscores

group by username;

討論:實現上述需求的關鍵在於,多個case when與group by的配合使用;下面我們來

分析下sql的執行過程:

我們手下看下select子句,可以確定要查詢的結果集中有5列,username、語文、數學、英

語、生物;拿出表中第一條記錄多資料處理得出的結果集:

username          語文數學英語生物

nick              80            0          0            0           

80是由case when subject='語文' then score else 0 end得出,其他的0分別是

由case when

subject='數學' then score else 0 end、

case when subject='英語' then score else 0 end、

case when subject='生物' then score else 0 end得出,一次類推,得到的結果集為

username          語文數學英語生物

nick               80       0       0         0

nike               0        90      0         0

nike               0        0       70        0

nike                 0        0        0         85

kent                 80      0        0         0

kent                 0        90      0         0

kent                  0        0       70        0

kent                 0        0       0         85

下一步,聚合分組,最終完成任務。

令一種實現方式:

select

username,

sum(decode(subject,'語文',score,0)) as "語文",

sum(decode(subject,'數學',score,0)) as "數學",

sum(decode(subject,'英語',score,0)) as "英語",

sum(decode(subject,'生物',score,0)) as "生物"

from studentscores

group by username;

問題:實現列轉行

解決:1、準備資料:

create table scoresstudent 

as select

username,

sum(decode(subject,'語文

',score,0)) as "

語文",

sum(decode(subject,'數學

',score,0)) as "

數學",

sum(decode(subject,'英語

',score,0)) as "

英語",

sum(decode(subject,'生物

',score,0)) as "生物"

from studentscores

group by username;

目前的資料形式:

目標資料形式:

實現方式:

select username,'

語文' as subject,"

語文" as score from scoresstudent

union

select username,'

數學' as subject,"

數學" as score from scoresstudent

union

select username,'

英語' as subject,"

英語" as score from scoresstudent

union

select username,'

生物' as subject,"

生物" as score from scoresstudent;

列轉行,行轉列總結

此函式在oracle版本12g以後廢棄,不可使用 解決方法 列轉行 使用 sys connect by path 函式替代 listagg函式 自從since oracle 9i 開始,就可以通過 sys connect by path 函式實現將從父節點到當前行內容以 path 或者層次元素列表的...

Hive列轉行 行轉列

簡單說一下hive行轉列 列轉行的方式。這玩意sql,語法它對嗎?就擱這列轉行?浪費時間!先拿這個資料來實現乙個簡單的行轉列和列轉行 表名就都叫hero吧,英雄屬性 hero type 英雄名s hero names 英雄名 hero name 行轉列也就是上面的表1到表2。select hero ...

SQL 列轉行 行轉列 的方法

建立表 插入資料 insert into col to row user name,course,score values 張三 語文 58 insert into col to row user name,course,score values 張三 數學 34 insert into col t...