您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Oracle中行列轉(zhuǎn)換及pivot子句怎么用,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
一、11g以前的行列轉(zhuǎn)換方法
創(chuàng)建測(cè)試表:
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
列轉(zhuǎn)行:
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
行轉(zhuǎn)回列:
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自帶的行列轉(zhuǎn)換
使用pivot和unpivot使得行列轉(zhuǎn)換變的很簡(jiǎn)單。
列轉(zhuǎn)行:
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
行轉(zhuǎn)列:
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
再舉一個(gè)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是有限的、可窮舉的,如果是變化的、隨機(jī)添加的,則可在in后面使用any或者子查詢,此時(shí)查詢的結(jié)果是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))));
關(guān)于“Oracle中行列轉(zhuǎn)換及pivot子句怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。