Oracle ranks of the converted notes

Oracle ranks of the converted notes

Work often used in this scene, but found that every time forget how to do it, where Oracle will be special columns in a row turn summarized;

 

Dynamic PIVOT using column row transfer

            First create a test table course:

CREATE TABLE PS_T_LY_TEST_COURSE(

       id NUMBER(11) primary key,

       name varchar2(30),

       tutor_id varchar2(11),

       year number(4)

);

      Insert test data:

      insert into PS_T_LY_TEST_COURSE VALUES (1, 'language lessons', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (2, 'math', '10002', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (3, 'chemistry class', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (4, 'physics class', '10004', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (5, 'biology', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (6, 'PE', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (7, 'history lesson', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (8, 'geography lesson', '10002', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (9, 'social class', '10004', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (10, 'English classes', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (11, 'Japanese lessons', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (12, 'German class', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (13, 'French class', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (14, 'Russian class', '10003', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (15, 'science classes', '10002', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (16, 'philosophy class', '10002', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (17, 'law class', '10002', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (18, 'Accounting class', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (19, 'computer classes', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (20, 'life lessons', '10001', 2018);

insert into PS_T_LY_TEST_COURSE VALUES (21, 'language lessons +', '10001', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (22, 'math +', '10002', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (23, 'chemistry class +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (24, 'physics class +', '10004', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (25, 'biology +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (26, '+ PE', '10001', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (27, 'history lessons +', '10001', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (28, 'geography lesson +', '10002', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (29, 'Social Studies +', '10004', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (30, 'English classes +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (31, 'Japanese lessons +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (32, 'German classes +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (33, 'French class +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (34, 'Russian courses +', '10003', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (35, 'science classes +', '10002', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (36, 'philosophy class +', '10002', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (37, 'Legal Division +', '10002', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (38, 'Accounting class +', '10001', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (39, 'computer classes +', '10001', 2019);

insert into PS_T_LY_TEST_COURSE VALUES (40, 'life lessons +', '10001', 2019);

The number of courses each calendar year statistics tutor Professor

SELECT *

  FROM (SELECT YEAR, TUTOR_ID FROM PS_T_LY_TEST_COURSE) PIVOT(COUNT(1) FOR TUTOR_ID IN('10001' AS "导师1",

'10002' AS "tutor"

'10003' AS "tutor 3",

'10004' AS "tutor 4"));

 

 

DECODE switch using a dynamic row column

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 dynamically using column row transfer

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;

 

Well, introduce the above three methods, written in a very simple, there is no explanation, only for their own use records;

PS: For reprint please indicate the source!

Guess you like

Origin www.cnblogs.com/shyly5140/p/12106222.html