1. 程式人生 > >資料庫 行轉列 列轉行詳解

資料庫 行轉列 列轉行詳解

目錄結構如下:

  • 行轉列
  • 列轉行

[一]、行轉列

1.1、初始測試資料

表結構:TEST_TB_GRADE

createtable TEST_TB_GRADE   
  1. (   
  2.   ID        NUMBER(10) notnull,   
  3.   USER_NAME VARCHAR2(20 CHAR),   
  4.   COURSE    VARCHAR2(20 CHAR),   
  5.   SCORE     FLOAT
  6. )  
create table TEST_TB_GRADE
(
  ID        NUMBER(10) not null,
  USER_NAME VARCHAR2(20 CHAR),
  COURSE    VARCHAR2(20 CHAR),
  SCORE     FLOAT
)

 初始資料如下圖:


                       

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


                    

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

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

1.3、延伸

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


                

具體的實現sql如下:

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

[二]、列轉行

1.1、初始測試資料

        表結構:TEST_TB_GRADE2

createtable TEST_TB_GRADE2   
  1. (   
  2.   ID         NUMBER(10) notnull,   
  3.   USER_NAME  VARCHAR2(20 CHAR),   
  4.   CN_SCORE   FLOAT,   
  5.   MATH_SCORE FLOAT,   
  6.   EN_SCORE   FLOAT
  7. )  
create table TEST_TB_GRADE2
(
  ID         NUMBER(10) not null,
  USER_NAME  VARCHAR2(20 CHAR),
  CN_SCORE   FLOAT,
  MATH_SCORE FLOAT,
  EN_SCORE   FLOAT
)
 

        初始資料如下圖:


       

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


                      

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

select user_name, '語文' COURSE , CN_SCORE as SCORE from test_tb_grade2    
  1. unionselect user_name, '數學' COURSE, MATH_SCORE as SCORE from test_tb_grade2    
  2. unionselect user_name, '英語' COURSE, EN_SCORE as SCORE from test_tb_grade2    
  3. orderby user_name,COURSE   
select user_name, '語文' COURSE , CN_SCORE as SCORE from test_tb_grade2 
union select user_name, '數學' COURSE, MATH_SCORE as SCORE from test_tb_grade2 
union select user_name, '英語' COURSE, EN_SCORE as SCORE from test_tb_grade2 
order by user_name,COURSE 

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

createtable TEST_TB_GRADE3     
  1.     (    
  2.       USER_NAME VARCHAR2(20 CHAR),     
  3.       COURSE    VARCHAR2(20 CHAR),     
  4.       SCORE     FLOAT
  5.     )    
create table TEST_TB_GRADE3  
    ( 
      USER_NAME VARCHAR2(20 CHAR),  
      COURSE    VARCHAR2(20 CHAR),  
      SCORE     FLOAT  
    )  

 再執行下面的sql:

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

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

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