oracle 行轉列,列轉行
目錄結構如下:
- 行轉列
- 列轉行
[一]、行轉列
1.1、初始測試資料
表結構:TEST_TB_GRADE
Sql程式碼- createtable TEST_TB_GRADE
- (
- ID NUMBER(10) notnull,
- USER_NAME VARCHAR2(20 CHAR),
- COURSE VARCHAR2(20 CHAR),
- SCORE FLOAT
- )
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程式碼- select t.user_name,
- sum(decode(t.course, '語文', score,null)) as CHINESE,
- sum(decode(t.course, '數學', score,null)) as MATH,
- sum(decode(t.course,
- from test_tb_grade t
- groupby t.user_name
- 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程式碼- 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,
- casewhen t.score <60 then'00-60'
- when t.score >=60 and t.score <80 then'60-80'
- when t.score >=80 then'80-100'endas SCORE_GP,
- count(t.score) as COUNTNUM
- FROM test_tb_grade t
- groupby t.course,
- casewhen 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
- orderby t.course ) t2
- groupby t2.SCORE_GP
- 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程式碼- createtable TEST_TB_GRADE2
- (
- ID NUMBER(10) notnull,
- USER_NAME VARCHAR2(20 CHAR),
- CN_SCORE FLOAT,
- MATH_SCORE FLOAT,
- EN_SCORE FLOAT
- )
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程式碼- select user_name, '語文' COURSE , CN_SCORE as SCORE from test_tb_grade2
- unionselect user_name, '數學' COURSE, MATH_SCORE as SCORE from test_tb_grade2
- unionselect user_name, '英語' COURSE, EN_SCORE as SCORE from test_tb_grade2
- 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程式碼- createtable TEST_TB_GRADE3
- (
- USER_NAME VARCHAR2(20 CHAR),
- COURSE VARCHAR2(20 CHAR),
- SCORE FLOAT
- )
create table TEST_TB_GRADE3
(
USER_NAME VARCHAR2(20 CHAR),
COURSE VARCHAR2(20 CHAR),
SCORE FLOAT
)
再執行下面的sql:
Sql程式碼- insertall
- 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;
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,