Oracle 行列轉換筆記

2022-09-03 05:21:05 字數 4132 閱讀 7928

工作中經常會用到這個場景,但是發現每次都會忘記要怎麼做,這裡特將oracle進行行轉列進行乙個歸納總結;

使用pivot進行動態行轉列

首先建立乙個測試表course:

create table ps_t_ly_test_course(

id number(11) primary key,

name varchar2(30),

tutor_id varchar2(11),

year number(4)

插入測試資料:

insert into ps_t_ly_test_course values (1,'語文課','10001',2018);

insert into ps_t_ly_test_course values (2,'數學課','10002',2018);

insert into ps_t_ly_test_course values (3,'化學課','10003',2018);

insert into ps_t_ly_test_course values (4,'物理課','10004',2018);

insert into ps_t_ly_test_course values (5,'生物課','10003',2018);

insert into ps_t_ly_test_course values (6,'體育課','10001',2018);

insert into ps_t_ly_test_course values (7,'歷史課','10001',2018);

insert into ps_t_ly_test_course values (8,'地理課','10002',2018);

insert into ps_t_ly_test_course values (9,'社會課','10004',2018);

insert into ps_t_ly_test_course values (10,'英語課','10003',2018);

insert into ps_t_ly_test_course values (11,'日語課','10003',2018);

insert into ps_t_ly_test_course values (12,'德語課','10003',2018);

insert into ps_t_ly_test_course values (13,'法語課','10003',2018);

insert into ps_t_ly_test_course values (14,'俄語課','10003',2018);

insert into ps_t_ly_test_course values (15,'科學課','10002',2018);

insert into ps_t_ly_test_course values (16,'哲學課','10002',2018);

insert into ps_t_ly_test_course values (17,'法學課','10002',2018);

insert into ps_t_ly_test_course values (18,'會計課','10001',2018);

insert into ps_t_ly_test_course values (19,'計算機課','10001',2018);

insert into ps_t_ly_test_course values (20,'人生課','10001',2018);

insert into ps_t_ly_test_course values (21,'語文課+','10001',2019);

insert into ps_t_ly_test_course values (22,'數學課+','10002',2019);

insert into ps_t_ly_test_course values (23,'化學課+','10003',2019);

insert into ps_t_ly_test_course values (24,'物理課+','10004',2019);

insert into ps_t_ly_test_course values (25,'生物課+','10003',2019);

insert into ps_t_ly_test_course values (26,'體育課+','10001',2019);

insert into ps_t_ly_test_course values (27,'歷史課+','10001',2019);

insert into ps_t_ly_test_course values (28,'地理課+','10002',2019);

insert into ps_t_ly_test_course values (29,'社會課+','10004',2019);

insert into ps_t_ly_test_course values (30,'英語課+','10003',2019);

insert into ps_t_ly_test_course values (31,'日語課+','10003',2019);

insert into ps_t_ly_test_course values (32,'德語課+','10003',2019);

insert into ps_t_ly_test_course values (33,'法語課+','10003',2019);

insert into ps_t_ly_test_course values (34,'俄語課+','10003',2019);

insert into ps_t_ly_test_course values (35,'科學課+','10002',2019);

insert into ps_t_ly_test_course values (36,'哲學課+','10002',2019);

insert into ps_t_ly_test_course values (37,'法學課+','10002',2019);

insert into ps_t_ly_test_course values (38,'會計課+','10001',2019);

insert into ps_t_ly_test_course values (39,'計算機課+','10001',2019);

insert into ps_t_ly_test_course values (40,'人生課+','10001',2019);

統計歷年各個導師教授的課程數

select *

from (select year, tutor_id from ps_t_ly_test_course) pivot(count(1) for tutor_id in('10001' as "導師1",

'10002' as "導師2",

'10003' as "導師3",

'10004' as "導師4"));

使用decode進行動態行轉列

select year,

count(decode(tutor_id, '10001', id, null)) 導師1,

count(decode(tutor_id, '10002', id, null)) 導師2,

count(decode(tutor_id, '10003', id, null)) 導師3,

count(decode(tutor_id, '10004', id, null)) 導師4

from ps_t_ly_test_course

group by year;

使用case when進行動態行轉列

select year,

count(case tutor_id when '10001' then id else null end) 導師1,

count(case tutor_id when '10002' then id else null end) 導師2,

count(case tutor_id when '10003' then id else null end) 導師3,

count(case tutor_id when '10004' then id else null end) 導師4

from ps_t_ly_test_course

group by year;

好了就介紹以上三種方法,寫的很簡陋,也沒有什麼說明,只為自己記錄使用;

進製轉換 筆記

color red 一 十進位制數 d 的轉換 color 1 二進位制 b 八進位制 q 十六進製制數 h 轉十進位制 d 公式 二進位制。八進位制 十六進製制 十六進製制的各位數字分別乘以各自的基數 2,8,16 的 n 1 次方,其相加之和便是相應的十進位制數。例1 110b 1 2的2次方 ...

進製轉換筆記

先講一下定義吧,進製也就是進製位,對於接觸過電腦的人來說應該都不陌生,我們常用的進製包括 二進位制 八進位制 十進位制與十六進製制,它們之間區別在於數運算時是逢幾進一位。比如二進位制是逢2進一位,十進位制也就是我們常用的0 9是逢10進一位。其他的同理。好的,接下來就是進製之間的互相轉換了。二進位制...

Oracle行列轉換

1.列轉行 create table t col row id int,c1 varchar2 10 c2 varchar2 10 c3 varchar2 10 insert into t col row values 1,v11 v21 v31 insert into t col row valu...