SELECT Name, SUM(CASE WHEN Subject = '語文' THEN Result END) AS 語文,
SUM(CASE WHEN Subject = '數學' THEN Result END) AS 數學,
SUM(CASE WHEN Subject = '英語' THEN Result END) AS 英語
FROM CT
GROUP BY Name
二、Oracle中的轉置
現有一個商品銷售表sale,表結構爲:
month char(6) --月份
sell number(10,2) --月銷售金額
現有數據爲:
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
想要轉化爲以下結構的數據:
year char(4) --年份
month1 number(10,2) --1月銷售金額
month2 number(10,2) --2月銷售金額
month3 number(10,2) --3月銷售金額
month4 number(10,2) --4月銷售金額
month5 number(10,2) --5月銷售金額
month6 number(10,2) --6月銷售金額
month7 number(10,2) --7月銷售金額
month8 number(10,2) --8月銷售金額
month9 number(10,2) --9月銷售金額
month10 number(10,2) --10月銷售金額
month11 number(10,2) --11月銷售金額
month12 number(10,2) --12月銷售金額
結構轉化的SQL語句爲:
create or replace view
v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
as
select
substrb(month,1,4),
sum(decode(substrb(month,5,2),'01',sell,0)),
sum(decode(substrb(month,5,2),'02',sell,0)),
sum(decode(substrb(month,5,2),'03',sell,0)),
sum(decode(substrb(month,5,2),'04',sell,0)),
sum(decode(substrb(month,5,2),'05',sell,0)),
sum(decode(substrb(month,5,2),'06',sell,0)),
sum(decode(substrb(month,5,2),'07',sell,0)),
sum(decode(substrb(month,5,2),'08',sell,0)),
sum(decode(substrb(month,5,2),'09',sell,0)),
sum(decode(substrb(month,5,2),'10',sell,0)),
sum(decode(substrb(month,5,2),'11',sell,0)),
sum(decode(substrb(month,5,2),'12',sell,0))
from sale
group by substrb(month,1,4);
oracle sql語句轉置表
想把表1轉成表2,僅考慮用sql語句實現,謝謝。
select col1,col2,sum(decode(col3,'X',col4)) X
,sum(decode(col3,'Y',col4)) Y
,sum(decode(col3,'Z',col4)) Z
from tablename
group by col1,col2
其他回答 共2條
select a.列1,a.列2,a.列4 as X,null as Y,null as Z from 表1 as a where a.列3 = 'X'
union all
select b.列1,b.列2,null as X,b.列4 as Y,null as Z from 表1 as b where b.列3 = 'Y'
union all
select c.列1,c.列2,null as X,null as Y,c.列4 as Z from 表1 as c where c.列3 = 'Z'
select 列1,列2,
(case when 列3='x' then 列4 end) as x,(case when 列3='y' then 列4 end) as y,(case when 列3='z' then 列4 end) as z from 表1
你要顯示的漂亮一點就加:else ' '
select 列1,列2,
(case when 列3='x' then 列4 else ' 'end) as x,
(case when 列3='y' then 列4 else ' ' end) as y,
(case when 列3='z' then 列4 else ' ' end) as z
from 表1
例:住房公積金報表置換實例:
1.各個單位在本地經辦行進行開戶,開戶就是將單位的基本信息和職工信息的進行登記;
2.每月各個單位的會計到經辦行交繳本單位的所有職工的住房公積金,系統記錄有每個職工的交繳明細並在每條記錄上記錄有經辦行的代碼;
3.每月、季、半年及年終都要求將經辦行 變爲“列”給出個月的明細報表:
經辦行:城西區 城東區
月份:
2001.01 xxxx1.xx xxxxx2.xx
2001.02 xxxx3.xx xxxxx4.xx
。 。 。 。 。 。
原來的數據順序是:
城西區2001.01 xxxxx1.xx
城東區2001.01 xxxxx2.xx
城西區2001.02 xxxxx3.xx
城東區2001.02 xxxxx4.xx
住房公積金系統記錄職工的每月交繳名細的pay_lst表結構是:
bank_code varchar2(6)NOT NULL, -- 經辦行代碼 acc_no varchar2(15) not null, -- 單位代碼(單位帳號) emp_acc_no varchar2(20) not null, -- 職工帳號 tran_date date not null, -- 交繳日期 tran_val Number(7,2) not null, -- 交繳額 sys_date date default sysdate, --系統日期 oper_id varchar2(10) --操作員代碼 bank_code varchar2(6)NOT NULL, --經辦行代碼 acc_no varchar2(15) not null, --單位代碼(單位帳號) emp_acc_no varchar2(20) not null, --職工帳號 tran_date date not null, --交繳日期 tran_val Number(7,2) not null, --交繳額 sys_date date default sysdate, --系統日期 oper_id varchar2(10) --操作員代碼
這樣的表結構,一般按照將經辦行作爲行(row)進行統計是很容易的,
但是如果希望將經辦行變爲列(column)這樣的格式來輸出就有困難。
如果用DECODE函數來處理則變得很簡單:
我們創建一個視圖來對目前的pay_lst表進行查詢。
將經辦行代碼變爲一些具體的經辦行名稱即可:
CREATE OR REPLACE VIEW bank_date_lst AS Select to_char(tran_date,’yyyy.mm’), SUM( DECODE ( bank_code,’001’, tran_val,0 )) 城西區, SUM( DECODE ( bank_code,’002’, tran_val,0 )) 城南區, SUM( DECODE ( bank_code,’003’, tran_val,0 )) 城東區 FROM pay_lst GROUP BY to_char(tran_date,’yyyy.mm’); CREATE OR REPLACE VIEW bank_date_lst AS Select to_char(tran_date,’yyyy.mm’), SUM( DECODE ( bank_code,’001’, tran_val,0 ))城西區, SUM( DECODE ( bank_code,’002’, tran_val,0 ))城南區, SUM( DECODE ( bank_code,’003’, tran_val,0 ))城東區 FROM pay_lst GROUP BY to_char(tran_date,’yyyy.mm’);
建立視圖後,可直接對該視圖進行查詢就可按照列顯示出結果。