ITPub博客

Oracle行列转换及pivot子句的用法

原创 Oracle 作者:llnnmc 时间:2018-06-04 18:30:18 0 删除 编辑

一、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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-12-29

  • 博文量
    90
  • 访问量
    55588