在做项目经常遇到分科室、人员进行汇总的问题,在ORACLE中对此类问题的处理相当方便!下面以项目中遇到的实例进行说明:
查询语句如下:
select f_sys_getsectnamebysectid(a.sectionid) as sectname,
--a.sectionid,
f_sys_employinfo_getnamebyno(a.buyerid) as name,
sum(c.ratio) as llratio,
sum(b.ratio) as lratio,
sum(a.ratio) as ratio,
sum(d.ratio) as nratio
from (select sectionid, buyerid, ratio
from T_PPRM_ASPPurRatio
where year = '2008') a,
(select buyerid, ratio from T_PPRM_ASPPurRatio where year = '2007') b,
(select buyerid, ratio from T_PPRM_ASPPurRatio where year = '2006') c,
(select buyerid, ratio from T_PPRM_ASPPurRatio where year = '2009') d
where a.buyerid = b.buyerid(+)
and b.buyerid = c.buyerid(+)
and c.buyerid = d.buyerid(+)
group by rollup(a.sectionid, a.buyerid)
下面转帖更详细的介绍,需仔细体会
Oracle ROLLUP和CUBE 用法
Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。如果是ROLLUP(A, B, C)的话,首先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作。如果是GROUP BY CUBE(A, B, C),则首先会对(A、B、C)进行GROUP BY,然后依次是(A、B),(A、C),(A),(B、C),(B),(C),最后对全表进行GROUP BY操作。 grouping_id()可以美化效果:
Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。
除本文内容外,你还可参考:
分析函数参考手册: http://xsb.itpub.net/post/419/33028
分析函数使用例子介绍:http://xsb.itpub.net/post/419/44634
SQL> create table t as select * from dba_indexes;
�