1. 程式人生 > >Oracle 行列轉換函式pivot的使用(二)

Oracle 行列轉換函式pivot的使用(二)

關鍵函式pivot,其用法如下 pivot(聚合函式 for 列名 in(型別))

select * from table_name pivot(max(column_name)                            --行轉列後的列的值value,聚合函式是必須要有的
                               for column_name in(value_1,value_2,value_3)     --需要行轉列的列及其對應列的屬性1/2/3
                                     )

1、首先舉一個簡單的例子,建立一個數據表

1 create table tmp as select * from (
2 select '張三' student,'語文' course ,78 score from dual union all 
3 select '張三','數學',87 from dual union all 
4 select '張三','英語',82 from dual union all 
5 select '張三','物理',90 from dual union all 
6 select '李四','語文',65 from dual union all 
7
select '李四','數學',77 from dual union all 8 select '李四','英語',65 from dual union all 9 select '李四','物理',85 from dual);

先使用decode或case when方法

 1 select 
 2   student, 
 3   max(decode(course, '語文', score)) 語文, 
 4   max(decode(course, '數學', score)) 數學, 
 5   max(decode(course, '英語', score)) 英語, 
6 max(decode(course, '物理', score)) 物理, 7 sum(score) total 8 from tmp 9 group by student; 10 ----------------------------------------- 11 select 12 student, 13 max(case when course = '語文' then score end) 語文, 14 max(case when course = '數學' then score end) 數學, 15 max(case when course = '英語' then score end) 英語, 16 max(case when course = '物理' then score end) 物理, 17 sum(score) total 18 from tmp 19 group by student;

pivot的使用

1 select t.*,  
2   (t.語+t.數+t.外+t.物) as total  
3 from  
4   (select *  
5   from tmp pivot ( max(score) for course in ('語文' as 語 , '數學' as 數, '英語' as 外,'物理' as 物) )  
6   ) t;

結果同上

2、實際開發遇到的問題

有一張目標值表,年、月、日的值都是分開多行顯示,現需合併成一行顯示,具體資料如下:(type:1-->日,2-->月,3-->年;targetvalue:目標值)

select * from MOVEBI.T_GMS_MBI_TARGET_DATA where targetcode = '31227061'

 

此資料必須先進性處理,要保證資料可以聚合成一條,若直接使用會出現下列情況:

select * from MOVEBI.T_GMS_MBI_TARGET_DATA pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '31227061';

這不是我們想要的結果,具體改進法法如下:

 1 --方法一:對結果處理
 2 select max(datatime) datatime
 3       ,usercode
 4       ,deptcode
 5       ,deptname
 6       ,targetcode
 7       ,targetname
 8       ,sum(coalesce(day_value,0)) day_value
 9       ,sum(coalesce(mon_value,0)) mon_value
10       ,sum(coalesce(year_value,0)) year_value
11   from(
12 select datatime,usercode,deptcode,deptname,targetcode,targetname,day_value,mon_value,year_value
13   from MOVEBI.T_GMS_MBI_TARGET_DATA
14   pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '31227061')
15   group by usercode
16           ,deptcode
17           ,deptname
18           ,targetcode
19           ,targetname;
20 --方法二:對原始表處理
21 select *
22   from (select '20181017' datatime,
23                usercode,
24                deptcode,
25                deptname,
26                targetcode,
27                targetname,
28                targetvalue,
29                type
30           from MOVEBI.T_GMS_MBI_TARGET_DATA
31          where datatime in ('20181017', '201810')
32            and targetcode = '31227061') t
33            pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '31227061';

 

(注:此為學習記錄筆記,僅供參考若有問題請指正,後續補充......)

參考文件:https://www.cnblogs.com/harvey888/p/6735093.html

參考文件:https://www.cnblogs.com/markfeifei/p/4009343.html