Oracle中行列转换及pivot子句怎么用-成都快上网建站

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

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

十年的五指山网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整五指山建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“五指山网站设计”,“五指山网站推广”以来,每个客户项目都认真落实执行。

一、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子句怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


分享题目:Oracle中行列转换及pivot子句怎么用
网址分享:http://kswjz.com/article/ghpggp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流