Oracle开发之报表函数

作者:Paul Lin 时间:2023-07-23 16:29:00 

一、回顾一下前面《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,函数
0
投稿

猜你喜欢

  • js定时器怎么写?就是在特定时间执行某段程序

    2024-04-22 12:54:00
  • Python闭包与装饰器原理及实例解析

    2023-04-08 19:44:30
  • 基于JavaScript实现弹幕特效

    2024-04-22 22:30:23
  • 一个asp正则替换的方法

    2008-11-25 14:05:00
  • 在notepad++中实现直接运行python代码

    2022-08-19 09:52:00
  • Vue+Axios实现文件上传自定义进度条

    2024-05-29 22:24:57
  • 清除浮动的最简写法

    2009-03-30 15:58:00
  • vue cli2 和 cli3去掉eslint检查器报错的解决

    2024-05-29 22:23:31
  • Django unittest 设置跳过某些case的方法

    2022-02-28 21:46:13
  • python 函数的缺省参数使用注意事项分析

    2021-08-23 05:09:02
  • SqlServer中的日期与时间函数

    2011-11-03 17:12:34
  • Python3中多线程编程的队列运作示例

    2022-06-15 23:27:49
  • Python+DeOldify实现老照片上色功能

    2021-07-13 00:04:46
  • python 实现网上商城,转账,存取款等功能的信用卡系统

    2022-05-26 15:19:36
  • Oracle生成单据编号存储过程的实例代码

    2024-01-23 19:10:19
  • python3实现猜数字游戏

    2022-09-11 16:10:38
  • Vue+Mock.js模拟登录和表格的增删改查功能

    2024-05-29 22:20:04
  • SQL Server数据库服务器高性能设置

    2010-11-25 16:00:00
  • Django读取Mysql数据并显示在前端的实例

    2023-11-09 17:36:49
  • 系统默认的MySQL用户名消失的解决方法

    2008-12-02 14:26:00
  • asp之家 网络编程 m.aspxhome.com