码迷,mamicode.com
首页 > 数据库 > 详细

Oracle开发专题之:报表函数

时间:2015-06-02 20:07:40      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

目录
=========================================

1.报表函数简介
2.RATIO_TO_REPORT函数

一、报表函数简介:

回顾一下前面《Oracle开发专题之:窗口函数》中关于全统计一节,我们使用了Oracle提供的:

技术分享sum(sum(tot_sales)) over (order by month rows between unbounded preceding and unbounded following)


来统计全年的订单总额,这个函数会在记录集形成的过程中,每检索一条记录就执行一次,它总共执行了12次。这是非常费时的。实际上我们还有更简便的方法:

技术分享SQL> select month,
技术分享  2         sum(tot_sales) month_sales,
技术分享  3         sum(sum(tot_sales)) over(order by month
技术分享  4         rows between unbounded preceding and unbounded following) win_sales,
技术分享  5         over() rpt_sales
技术分享  6    from orders
技术分享  7   group by month;
技术分享
技术分享     MONTH MONTH_SALES WINDOW_SALES REPORT_SALES
技术分享---------- ----------- ------------ ------------
技术分享         1      610697      6307766      6307766
技术分享         2      428676      6307766      6307766
技术分享         3      637031      6307766      6307766
技术分享         4      541146      6307766      6307766
技术分享         5      592935      6307766      6307766
技术分享         6      501485      6307766      6307766
技术分享         7      606914      6307766      6307766
技术分享         8      460520      6307766      6307766
技术分享         9      392898      6307766      6307766
技术分享        10      510117      6307766      6307766
技术分享        11      532889      6307766      6307766
技术分享        12      492458      6307766      6307766
技术分享
技术分享已选择12行。


over函数的空括号表示该记录集的所有记录都应该被列入统计的范围,如果使用了partition by则先分区,再依次统计各个分区。

二、RATIO_TO_REPORT函数:

报表函数特(窗口函数)特别适合于报表中需要同时显示详细数据和统计数据的情况。例如在销售报告中经常会出现这样的需求:列出上一年度每个月的销售总额、年底销售额以及每个月的销售额占全年总销售额的比例:

方法①:

技术分享select all_sales.*,
技术分享           100 * round(cust_sales / region_sales, 2|| % Percent
技术分享 from (select o.cust_nbr customer,
技术分享                        o.region_id region,
技术分享                       sum(o.tot_sales) cust_sales,
技术分享                       sum(sum(o.tot_sales)) over(partition by o.region_id) region_sales
技术分享               from orders_tmp o
技术分享            where o.year = 2001
技术分享             group by o.region_id, o.cust_nbr) all_sales
技术分享 where all_sales.cust_sales > all_sales.region_sales * 0.2;


这是一种笨方法也是最易懂的方法。

方法②:

技术分享select region_id, salesperson_id, 
技术分享           sum(tot_sales) sp_sales,
技术分享           sum(sum(tot_sales)
                      over (partition by region_id), 2) percent_of_region
技术分享  from orders
技术分享where year = 2001
技术分享 group by region_id, salesperson_id
技术分享 order by region_id, salesperson_id;


方法③

技术分享select region_id, salesperson_id, 
技术分享            sum(tot_sales) sp_sales,
技术分享                                      over (partition by region_id), 2) sp_ratio
技术分享   from orders
技术分享where year = 2001
技术分享group by region_id, salesperson_id
技术分享order by region_id, salesperson_id;


Oracle提供的Ratio_to_report函数允许我们计算每条记录在其对应记录集或其子集中所占的比例。

参考资料:《Mastering Oracle SQL》(By Alan Beaulieu, Sanjay Mishra O‘Reilly June 2004  0-596-00632-2) 

Oracle开发专题之:报表函数

标签:

原文地址:http://www.cnblogs.com/liyong-hit/p/4547469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!