1. 程式人生 > >oracle 行轉列,列轉行

oracle 行轉列,列轉行

目錄結構如下:

  • 行轉列
  • 列轉行

[一]、行轉列

1.1、初始測試資料

表結構:TEST_TB_GRADE

Sql程式碼 複製程式碼 收藏程式碼
  1. createtable TEST_TB_GRADE   
  2. (   
  3.   ID        NUMBER(10) notnull,   
  4.   USER_NAME VARCHAR2(20 CHAR),   
  5.   COURSE    VARCHAR2(20 CHAR),   
  6.   SCORE     FLOAT
  7. )  
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如下:

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. groupby t.user_name   
  7. 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如下:

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. casewhen 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'endas SCORE_GP,   
  10. count(t.score) as COUNTNUM   
  11. FROM test_tb_grade t   
  12. groupby t.course,    
  13. casewhen 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. orderby t.course ) t2   
  17. groupby t2.SCORE_GP   
  18. 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

Sql程式碼 複製程式碼 收藏程式碼
  1. createtable TEST_TB_GRADE2   
  2. (   
  3.   ID         NUMBER(10) notnull,   
  4.   USER_NAME  VARCHAR2(20 CHAR),   
  5.   CN_SCORE   FLOAT,   
  6.   MATH_SCORE FLOAT,   
  7.   EN_SCORE   FLOAT
  8. )  
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語句如下:

Sql程式碼 複製程式碼 收藏程式碼
  1. select user_name, '語文' COURSE , CN_SCORE as SCORE from test_tb_grade2    
  2. unionselect user_name, '數學' COURSE, MATH_SCORE as SCORE from test_tb_grade2    
  3. unionselect user_name, '英語' COURSE, EN_SCORE as SCORE from test_tb_grade2    
  4. 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:

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

 再執行下面的sql:

Sql程式碼 複製程式碼 收藏程式碼
  1. insertall
  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;  
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,發現表中的資料就是列轉成行了。

相關推薦

oracle 轉行

目錄結構如下: 行轉列 列轉行 [一]、行轉列 1.1、初始測試資料 表結構:TEST_TB_GRADE Sql程式碼   createtable TEST_TB_GRADE   (     ID        NUMBER(10) notnull,

SQL Server 轉行

結果 name pre toolbar des null 表名 再次 arch 一、多行轉成一列(並以","隔開) 表名:A 表數據: 想要的查詢結果: 查詢語句: SELECT name , value = ( STUFF(( SELECT

轉行圖一圖二或圖二圖一

col ont rom clas 轉行 chinese hang cor div 圖一: Nam Course Score zhangsan Chinese 85 zhangsan Maths 76 zhangsan English 80 lisi C

oracle

問題描述:    應公司要求,設計功能,一個ID,對應不同的值,展示的時候不同的值拼接展示,如何實現;   解決思路:     1) 拼接字串,想到了 oracle  Function(),這樣肯定能實現,但是比較麻煩;

awk 處理文字:轉行

  [[email protected] ~]# cat f 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 51 2 3 4 5   awk '{for(i=1;i<=NF;i++)a[NR,i]=$i}END{for(j

oracle轉行、連續日期數字實現方式及mybatis下實現方式

九月份複習,十月份考試,十月底一直沒法收心,趕在十一初 由於不可抗拒的原因又不得不重新找工作就;欸~, 又是一番折騰,從入職到現在,可又沒法閒下來了... 這次就簡單介紹下oracle資料庫下如何實現行轉列、列轉行及此在mybatis中的實現方式,就具體用法我就不詳細說了,主要介紹下實戰中所碰到的坑

Oracle pivot 、轉行unpivot 的Sql語句總結

多行轉字串 這個比較簡單,用||或concat函式可以實現 print? 1.  select concat(id,username) str from app_user   2.     3.  select id||username str from app_use

資料庫錶轉行終極方案

--行轉列問題--建立測試環境Create Table TEST(DATES Varchar(6), EMPNO Varchar(5), STYPE Varchar(1), AMOUNT Int)--插入資料Insert TEST Select '200605',  '024

【python pandas】資料框轉行

測試資料: context_id subject_gmt differtime browse_count click_count like_count commet_count reply_count score_value last1

SQL Server 轉行。多成一

一、多行轉成一列(並以","隔開)表名:A表資料:想要的查詢結果:查詢語句:SELECT name , value = ( STUFF(( SELECT ',' + value FROM A

oracle轉行

行轉列:PIVOT列轉行:UNPIVOT這兩個是在oracle11g上面新增的函式。下面舉例說明用法。PIVOT:學生成績表,原資料:select class_name, student_name, course_type, result, created_date fr

老生常談之SQL Server (轉行

1 --靜態的行轉列 2 --新建一個科目成績表 3 --三個欄位:學生名稱,科目,成績 4 CREATE TABLE SubjectScore 5 ( 6 StuName nvarchar(20), 7 SubjectName nvarchar(20), 8 F

Hive轉行

下面舉兩個例子: 例一: 行轉列 資料: a b 1 a c 2 a b 3 c d 4 c d 5 c d 6 轉化為: a b 1,2,3 c d 4,5,6  創表 Hive>create table test1 (col1 String,col2 Strin

Oracle

cat pre case nbsp 運用 分隔 nvl ase partition 一、簡易運用 ——>沒轉之前一個主號綁定多個副號的多行輸出(像移動的歡樂在線) SELECT f.town_name 鎮區, f.school_name 學校,

Oracle+排序

--1.刪除臨時表 drop table biz_bus_station_direct_0711; --2.將站點資料等放入臨時表 create table biz_bus_station_direct

oracle 例子程式碼

select * from (select deptno,job,sal from emp) pivot(        sum(sal)        for job in (          'ANALYST' as analyst_sal,          'MA

偶遇Oracle

行轉列應該是資料庫比較常見的操作了,在oracle中可以使用pivot、decode,可以參考呆瓜的blog: SELECT name, MAX(DECODE(course, 'j

Oracle 小結

      最近在工作中,對行轉列進行了應用,在此做一個簡單的小結。       轉換過程如下:     1、建立表結構 CREATE TABLE RowToCol ( ID NUMBE

Oracle pivot函式基本用法

2018年9月30日22點,眼看著就10月份了,回頭看下,8月份就寫了一篇部落格,9月一篇都沒寫,想著還是得續一續。 剛好前幾天,幫一個群友處理了一個關於Oracle中行轉列,根據查詢中有的專案,動態轉列的做法。想著也挺好玩,不過看下時間,不太充足。所以暫時先寫個Oracl

oracle 的通用過程

createorreplaceprocedureproc(tabname invarchar2, col1 invarchar2, col2 invarchar2,