Oracle行列轉換小結


目錄結構如下:

  • 行轉列
  • 列轉行

[一]、行轉列

 

1.1、初始測試數據

 

表結構:TEST_TB_GRADE

 

Sql代碼  收藏代碼
  1. create table TEST_TB_GRADE  
  2. (  
  3.   ID        NUMBER(10) not null,  
  4.   USER_NAME VARCHAR2(20 CHAR),  
  5.   COURSE    VARCHAR2(20 CHAR),  
  6.   SCORE     FLOAT  
  7. )  

 初始數據如下圖:


                        

 

1.2、 如果需要實現如下的查詢效果圖:


                     

 

這就是最常見的行轉列,主要原理是利用decode函數、聚集函數(sum),結合group by分組實現的,具體的sql如下:

Sql代碼  收藏代碼
  1. select t.user_name,  
  2.   sum(decode(t.course, '語文', score,null)) as CHINESE,  
  3.   sum(decode(t.course, '數學', score,null)) as MATH,  
  4.   sum(decode(t.course, '英語', score,null)) as ENGLISH  
  5. from test_tb_grade t  
  6. group by t.user_name  
  7. order by t.user_name  
 

 

1.3、延伸

 

如果要實現對各門功課的不同分數段進行統計,效果圖如下:


                 

 

具體的實現sql如下:

Sql代碼  收藏代碼
  1. select t2.SCORE_GP,  
  2.   sum(decode(t2.course, '語文', COUNTNUM,null)) as CHINESE,  
  3.   sum(decode(t2.course, '數學', COUNTNUM,null)) as MATH,  
  4.   sum(decode(t2.course, '英語', COUNTNUM,null)) as ENGLISH  
  5. from (  
  6.   select t.course,  
  7.          case when t.score  <60 then '00-60'  
  8.               when t.score >=60 and t.score <80  then '60-80'  
  9.               when t.score >=80 then '80-100' end as SCORE_GP,  
  10.          count(t.score) as COUNTNUM  
  11.   FROM test_tb_grade t  
  12.   group by t.course,   
  13.         case when t.score  <60  then '00-60'  
  14.               when t.score >=60 and t.score <80  then '60-80'  
  15.               when t.score >=80 then '80-100' end  
  16.   order by t.course ) t2  
  17. group by t2.SCORE_GP  
  18. order by t2.SCORE_GP  
 

[二]、列轉行

 

1.1、初始測試數據

        表結構:TEST_TB_GRADE2

Sql代碼  收藏代碼
  1. create table TEST_TB_GRADE2  
  2. (  
  3.   ID         NUMBER(10) not null,  
  4.   USER_NAME  VARCHAR2(20 CHAR),  
  5.   CN_SCORE   FLOAT,  
  6.   MATH_SCORE FLOAT,  
  7.   EN_SCORE   FLOAT  
  8. )  
 

        初始數據如下圖:


        

 

1.2、 如果需要實現如下的查詢效果圖:


                       

 

這就是最常見的列轉行,主要原理是利用SQL裏面的union,具體的sql語句如下:

Sql代碼  收藏代碼
  1. select user_name, '語文' COURSE , CN_SCORE as SCORE from test_tb_grade2   
  2. union select user_name, '數學' COURSE, MATH_SCORE as SCORE from test_tb_grade2   
  3. union select user_name, '英語' COURSE, EN_SCORE as SCORE from test_tb_grade2   
  4. order by user_name,COURSE   

 

 也可以利用【 insert all into ... select 】來實現,首先需要先建一個表TEST_TB_GRADE3:

Sql代碼  收藏代碼
  1. create table TEST_TB_GRADE3    
  2.     (   
  3.       USER_NAME VARCHAR2(20 CHAR),    
  4.       COURSE    VARCHAR2(20 CHAR),    
  5.       SCORE     FLOAT    
  6.     )    

 再執行下面的sql:

 

Sql代碼  收藏代碼
  1. insert all  
  2. into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '語文', CN_SCORE)  
  3. into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '數學', MATH_SCORE)  
  4. into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, '英語', EN_SCORE)  
  5. select user_name, CN_SCORE, MATH_SCORE, EN_SCORE from test_tb_grade2;  
  6. commit;  

 別忘記commit操作,然後再查詢TEST_TB_GRADE3,發現表中的數據就是列轉成行了。

 

 

本文連接:http://sjsky.iteye.com/blog/1152167

 

 

轉載請註明來自:Michael's blog @ http://sjsky.iteye.com 

發佈了37 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章