Oracle报表函数——RATIO_TO_REPORT函数

楼主
我是社区第13859位番薯,欢迎点我头像关注我哦~
报表函数特(窗口函数)特别适合于报表中需要同时显示详细数据和统计数据的情况。例如在销售报告中经常会出现这样的需求:列出上一年度每个月的销售总额、年底销售额以及每个月的销售额占全年总销售额的比例。Oracle报表函数——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函数允许我们计算每条记录在其对应记录集或其子集中所占的比例。

分享扩散:

沙发
发表于 2012-9-25 17:38:29
感谢分享            
板凳
发表于 2012-10-23 13:47:17
学习了,谢谢分享!!!:)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部 返回列表