Oracle开发之报表函数

所属分类: 数据库 / oracle 阅读数: 1425
收藏 0 赞 0 分享

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

复制代码 代码如下:
sum(sum(tot_sales)) over (order by month rows between unbounded preceding and unbounded following)

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

复制代码 代码如下:
SQL> select month,
         sum(tot_sales) month_sales,
         sum(sum(tot_sales)) over(order by month
         rows between unbounded preceding and unbounded following) win_sales,
         sum(sum(tot_sales)) over() rpt_sales
    from orders
   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,
           round(sum(tot_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,
            round(ratio_to_report(sum(tot_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函数允许我们计算每条记录在其对应记录集或其子集中所占的比例。

以上就是Oracle报表函数用法的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

oracle存储过程中return和exit区别概述及测试

至于return和exit在oracle存储过程中的应用,有些新手朋友们还是比较容易混淆的,本文将针对这两个关键字进行详细对比下,感兴趣的你可以参考下,希望可以帮助到你
收藏 0 赞 0 分享

oracle查看当前日期是第几个星期的方法

oracle查看当前日期是第几个星期方法的代码段,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle删除已存在的表的实例

查询系统表,判断表是否存在,存在则直接删除
收藏 0 赞 0 分享

oracle中文乱码解决的办法

oracle中文乱码解决的办法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle中在pl/sql developer修改表的2种方法

Oracle中在pl/sql developer修改表的2种方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

oracle 创建表空间步骤代码

oracle 创建表空间步骤代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

Oracle 查看表空间的大小及使用情况sql语句

表空间使用情况包括:查看表空间的名称及大小/查看表空间物理文件的名称及大小/查看回滚段名称及大小等等感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle Form中COMMIT的概述及使用技巧

针对form上面的数据变动提交到后台数据库,同时数据库提交数据,接下来将详细介绍下Form中COMMIT的使用,感兴趣的你可以参考下本文
收藏 0 赞 0 分享

Oracle跨数据库查询并插入实现原理及代码

需要从一个数据库中的表GIS_WEICHAI_DATA_1S中的数据导入到另个一数据库的表GIS_WEICHAI_DATA_1S中,接下来为你讲解跨数据库查询并插入需要的朋友可以参考下
收藏 0 赞 0 分享

Oracle 存储过程发送邮件实例学习

接下来将介绍下如何使用存储过程发送邮件这一案例实现,感兴趣的你可以参考下本文或许对你有所帮助
收藏 0 赞 0 分享
查看更多