1. 程式人生 > >Oracle pivot 與unpivot的用法

Oracle pivot 與unpivot的用法

好像要註冊一個Oracle賬號

create table olympic_medal_winners (   
  olympic_year int,  
  sport        varchar2( 30 ),  
  gender       varchar2( 1 ),  
  event        varchar2( 128 ),  
  medal        varchar2( 10 ),  
  noc          varchar2( 3 ),  
  athlete      varchar2( 128 ) 
);


Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Individual','Gold','KOR','KU Bonchan');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Individual','Silver','FRA','VALLADONT Jean-Charles');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Individual','Bronze','USA','ELLISON Brady');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Team','Gold','KOR','Republic of Korea');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Team','Bronze','AUS','Australia');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Archery','M','Men''s Team','Silver','USA','United States');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Artistic Gymnastics','M','Men''s Floor Exercise','Gold','GBR','WHITLOCK Max');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Artistic Gymnastics','M','Men''s Floor Exercise','Bronze','BRA','MARIANO Arthur');

Insert into olympic_medal_winners (OLYMPIC_YEAR,SPORT,GENDER,EVENT,MEDAL,NOC,ATHLETE) values (2016,'Artistic Gymnastics','M','Men''s Floor Exercise','Silver','BRA','HYPOLITO Diego');






SQL> select * from olympic_medal_winners;
OLYMPIC_YE SPORT                          GENDER EVENT                MEDAL      NOC ATHLETE
---------- ------------------------------ ------ -------------------- ---------- --- ------------------------------
      2016 Archery                        M      Men's Individual     Gold       KOR KU Bonchan
      2016 Archery                        M      Men's Individual     Silver     FRA VALLADONT Jean-Charles
      2016 Archery                        M      Men's Individual     Bronze     USA ELLISON Brady
      2016 Archery                        M      Men's Team           Gold       KOR Republic of Korea
      2016 Archery                        M      Men's Team           Bronze     AUS Australia
      2016 Archery                        M      Men's Team           Silver     USA United States
      2016 Artistic Gymnastics            M      Men's Floor Exercise Gold       GBR WHITLOCK Max
      2016 Artistic Gymnastics            M      Men's Floor Exercise Bronze     BRA MARIANO Arthur
      2016 Artistic Gymnastics            M      Men's Floor Exercise Silver     BRA HYPOLITO Diego
 
'
#####################################################################################################################################################################################        
#計算獎牌數
select * from olympic_medal_winners   
pivot ( count(*) for medal in (   
 'Gold' gold, 'Silver' silver, 'Bronze' bronze   
))   
order by noc;


OLYMPIC_YE SPORT                          GENDER EVENT                NOC ATHLETE                              GOLD     SILVER     BRONZE
---------- ------------------------------ ------ -------------------- --- ------------------------------ ---------- ---------- ----------
      2016 Archery                        M      Men's Team           AUS Australia                               0          0          1
      2016 Artistic Gymnastics            M      Men's Floor Exercise BRA HYPOLITO Diego                          0          1          0
      2016 Artistic Gymnastics            M      Men's Floor Exercise BRA MARIANO Arthur                          0          0          1
      2016 Archery                        M      Men's Individual     FRA VALLADONT Jean-Charles                  0          1          0
      2016 Artistic Gymnastics            M      Men's Floor Exercise GBR WHITLOCK Max                            1          0          0
      2016 Archery                        M      Men's Individual     KOR KU Bonchan                              1          0          0
      2016 Archery                        M      Men's Team           KOR Republic of Korea                       1          0          0
      2016 Archery                        M      Men's Individual     USA ELLISON Brady                           0          0          1
      2016 Archery                        M      Men's Team           USA United States                           0          1          0
         
'

SQL> select * from (
  2    select noc, medal, sport, event, gender, athlete
  3    from   olympic_medal_winners
  4  )
  5  pivot  (
  6    count( distinct sport ||'#'|| event ||'#'|| gender ) medals,
  7    count( distinct sport ) sports,
  8    listagg( athlete, ',') within group (order by athlete) athletes
  9    for medal in ( 'Gold' gold )
 10  )
 11  where  gold_medals > 1
 12  order  by gold_medals, gold_sports, noc;

NOC GOLD_MEDALS GOLD_SPORTS GOLD_ATHLETES
--- ----------- ----------- --------------------------------------------------------------------------------
KOR           2           1 KU Bonchan,Republic of Korea




####################################################################################################################################################################################        


create table olympic_medal_tables as   
       select * from (   
                     select noc, medal, sport, event, gender   
                        from olympic_medal_winners   
                     )   
          pivot ( count(distinct sport ||'#'|| event ||'#'||gender ) for medal in 
                            (   
                              'Gold' gold_medals, 'Silver' silver_medals, 'Bronze' bronze_medals   
                            )
               )   
              order  by 2 desc, 3 desc, 4 desc;

SQL> select * from olympic_medal_tables
  2  ;

NOC GOLD_MEDALS SILVER_MEDALS BRONZE_MEDALS
--- ----------- ------------- -------------
KOR           2             0             0
GBR           1             0             0
BRA           0             1             1
USA           0             1             1
FRA           0             1             0
AUS           0             0             1


select * from olympic_medal_tables   
              unpivot (medal_count for medal_colour in 
                          (   
                          gold_medals as 'GOLD',   
                          silver_medals as 'SILVER',   
                          bronze_medals as 'BRONZE'   
                          )
                      )   
                 order  by noc;

SQL> select * from olympic_medal_tables
  2  unpivot (medal_count for medal_colour in (
  3    gold_medals as 'GOLD',
  4    silver_medals as 'SILVER',
  5    bronze_medals as 'BRONZE'
  6  ))
  7  order  by noc;

NOC MEDAL_COLOUR MEDAL_COUNT
--- ------------ -----------
AUS SILVER                 0
AUS GOLD                   0
AUS BRONZE                 1
BRA BRONZE                 1
BRA SILVER                 1
BRA GOLD                   0
FRA SILVER                 1
FRA GOLD                   0
FRA BRONZE                 0
GBR SILVER                 0
GBR BRONZE                 0
GBR GOLD                   1
KOR SILVER                 0
KOR BRONZE                 0
KOR GOLD                   2
USA SILVER                 1
USA GOLD                   0
USA BRONZE                 1