首页 > 代码库 > Oracle pivot & unpivot

Oracle pivot & unpivot

pivot & unpivot 11g新特性

1     pivot

-对的形式出现,典型的行转列报表函数

create table test_demo(id int,name varchar(20),nums int);  ---- 创建表
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聚合函数 for 列名 in 类型 ,其中 in 中可以指定别名in中还可以指定子查询,比如 select distinct code from customers

2     unpivot

典型的列转行报表函数

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没有聚合函数,quartersell字段也是临时的变量。

这里sell是统计值,quarter表示季度及类型。

执行结果:

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


本文出自 “90SirDB” 博客,请务必保留此出处http://90sirdb.blog.51cto.com/8713279/1928115

Oracle pivot & unpivot