pivot & unpivot 11g新特性

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比濠江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式濠江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋濠江地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
以列-值對的形式出現(xiàn),典型的行轉(zhuǎn)列報表函數(shù)。
create table test_demo(id int,name varchar(20),nums int); ---- 創(chuàng)建表
insert into test_demo values(1, '蘋果', 1000);
insert into test_demo values(2, '蘋果', 2000);
insert into test_demo values(3, '蘋果', 4000);
insert into test_demo values(4, '橘子', 5000);
insert into test_demo values(5, '橘子', 3000);
insert into test_demo values(6, '葡萄', 3500);
insert into test_demo values(7, '芒果', 4200);
insert into test_demo values(8, '芒果', 5500);
commit;
select name, sum(nums)
from test_demo
group by name;
select *
from (select name, nums fromtest_demo)
pivot(sum(nums)
for name in('蘋果', '橘子', '葡萄', '芒果'));
SQL> select *
2 from (select name, nums from test_demo)
3 pivot(sum(nums)
4 for name in('蘋果' as "蘋果", '橘子', '葡萄', '芒果')); --別名使用
蘋果 '橘子' '葡萄' '芒果'
---------- ---------- ---------- ----------
7000 8000 3500 9700
這里再說語法:
pivot聚合函數(shù) for列名 in類型 ,其中 in 中可以指定別名,in中還可以指定子查詢,比如 select distinct code from customers
典型的列轉(zhuǎn)行報表函數(shù)
create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int,Q4 int); 這里Q1 int, Q2int, Q3 int, Q4 int表示四季度。 insert into Fruit values(1,'蘋果',1000,2000,3300,5000); insert into Fruit values(2,'橘子',3000,3000,3200,1500); insert into Fruit values(3,'香蕉',2500,3500,2200,2500); insert into Fruit values(4,'葡萄',1500,2500,1200,3500); commit; select * from Fruit; select id , name, quarter, sell from Fruit unpivot (sell for quarterin (q1, q2, q3, q4));
注意:unpivot沒有聚合函數(shù),quarter、sell字段也是臨時的變量。
這里sell是統(tǒng)計值,quarter表示季度及類型。
執(zhí)行結(jié)果:
SQL> select id , name, quarter, sell from Fruit unpivot (sell forquarter in (q1, q2, q3, q4)); ID NAME QUARTER SELL --------------------------------------- -------------------- ---------------------------------------------- 1 蘋果 Q1 1000 1 蘋果 Q2 2000 1 蘋果 Q3 3300 1 蘋果 Q4 5000 2 橘子 Q1 3000 2 橘子 Q2 3000 2 橘子 Q3 3200 2 橘子 Q4 1500 3 香蕉 Q1 2500 3 香蕉 Q2 3500 3 香蕉 Q3 2200 3 香蕉 Q4 2500 4 葡萄 Q1 1500 4 葡萄 Q2 2500 4 葡萄 Q3 1200 4 葡萄 Q4 3500
新聞標題:Oraclepivot&unpivot
URL分享:http://www.yijiale78.com/article44/pceohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、全網(wǎng)營銷推廣、建站公司、用戶體驗、外貿(mào)建站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)