Oracle行列轉換及pivot子句的用法

llnnmc發表於2018-06-04

一、11g以前的行列轉換方法

建立測試表:

create table emp_phone(name varchar2(50), type char, phone varchar2(50));

insert into emp_phone values('張三', '1', '1234-5678');
insert into emp_phone values('張三', '2', '3219-6066');
insert into emp_phone values('張三', '3', '5365-9583');
insert into emp_phone values('李四', '1', '6837-2745');
insert into emp_phone values('李四', '3', '2649-5820');
insert into emp_phone values('王五', '1', '5838-9002');
insert into emp_phone values('王五', '2', '2749-5580');
insert into emp_phone values('陳六', '2', '9876-3453');
commit;

select * from emp_phone;

NAME                 T PHONE
-------------------- - --------------------
張三                 1 1234-5678
張三                 2 3219-6066
張三                 3 5365-9583
李四                 1 6837-2745
李四                 3 2649-5820
王五                 1 5838-9002
王五                 2 2749-5580
陳六                 2 9876-3453

列轉行:

select name, max(decode(type, '1', phone)) home, max(decode(type, '2', phone)) office, max(decode(type, '3', phone)) mobile from emp_phone group by name;

NAME                 HOME                 OFFICE               MOBILE
-------------------- -------------------- -------------------- --------------------
陳六                                      9876-3453
王五                 5838-9002            2749-5580
李四                 6837-2745                                 2649-5820
張三                 1234-5678            3219-6066            5365-9583

行轉回列:

create table emp_phone1 as
(select name, max(decode(type, '1', phone)) home, max(decode(type, '2', phone)) office, max(decode(type, '3', phone)) mobile from emp_phone group by name);
select * from emp_phone1;

NAME                 HOME                 OFFICE               MOBILE
-------------------- -------------------- -------------------- --------------------
陳六                                      9876-3453
王五                 5838-9002            2749-5580
李四                 6837-2745                                 2649-5820
張三                 1234-5678            3219-6066            5365-9583

select name, decode(lvl, 1, home, 2, office, 3, mobile) phone
  from emp_phone1, (select level lvl from dual connect by level <= 3)
 where decode(lvl, 1, home, 2, office, 3, mobile) is not null
 order by name;

NAME                 PHONE
-------------------- --------------------
陳六                 9876-3453
李四                 6837-2745
李四                 2649-5820
王五                 2749-5580
王五                 5838-9002
張三                 1234-5678
張三                 5365-9583
張三                 3219-6066

二、11g自帶的行列轉換

使用pivot和unpivot使得行列轉換變的很簡單。

列轉行:
select * from emp_phone pivot(max(phone) for type in (1 as home, 2 as office, 3 as mobile));

NAME                 HOME                 OFFICE               MOBILE
-------------------- -------------------- -------------------- --------------------
陳六                                      9876-3453
王五                 5838-9002            2749-5580
李四                 6837-2745                                 2649-5820
張三                 1234-5678            3219-6066            5365-9583

行轉列:
select * from emp_phone1 unpivot(phone for type in (home as 1, office as 2, mobile as 3));

NAME                       TYPE PHONE
-------------------- ---------- --------------------
陳六                          2 9876-3453
王五                          1 5838-9002
王五                          2 2749-5580
李四                          1 6837-2745
李四                          3 2649-5820
張三                          1 1234-5678
張三                          2 3219-6066
張三                          3 5365-9583

再舉一個Oracle示例方案scott中的例子
select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10

列出各部門的工資總和
select * from (select sal, deptno from emp) pivot(sum(sal) for deptno in (10 as dept_10, 20 as dept_20, 30 as dept_30));

   DEPT_10    DEPT_20    DEPT_30
---------- ---------- ----------
      8750      10875       9400

這裡deptno是有限的、可窮舉的,如果是變化的、隨機新增的,則可在in後面使用any或者子查詢,此時查詢的結果是XML格式串
select *
  from ((select sal, deptno from emp) pivot
        xml(sum(sal) for deptno in (any)));

如果是用子查詢
select *
  from ((select sal, deptno from emp) pivot
        xml(sum(sal) for deptno in (select deptno from emp where deptno in (10, 20, 30))));

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28974745/viewspace-2155620/,如需轉載,請註明出處,否則將追究法律責任。

相關文章