温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle中行列转换及pivot子句怎么用

发布时间:2021-11-11 14:20:03 来源:亿速云 阅读:170 作者:小新 栏目:关系型数据库

这篇文章将为大家详细讲解有关Oracle中行列转换及pivot子句怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、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))));

Oracle中行列转换及pivot子句怎么用

关于“Oracle中行列转换及pivot子句怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI