posts - 262,  comments - 221,  trackbacks - 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         sum(sum(tot_sales)) 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,
                     
          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函數允許我們計算每條記錄在其對應記錄集或其子集中所占的比例。

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


          -------------------------------------------------------------
          生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
          posted on 2008-06-29 12:02 Paul Lin 閱讀(5798) 評論(3)  編輯  收藏 所屬分類: Oracle 開發


          FeedBack:
          # re: 【原】Oracle開發專題之:報表函數
          2008-09-03 15:54 | yz
          博主的ORACLE開發專題,很值得學習,謝謝了!  回復  更多評論
            
          # re: 【原】Oracle開發專題之:報表函數
          2010-09-01 16:53 | 主程序
          學了不少東西,感謝  回復  更多評論
            
          # re: 【原】Oracle開發專題之:報表函數
          2014-04-17 10:53 | wffger
          證件信息zjxx表,記錄有經辦人jbr,證件種類zjzl,車輛種類clzl 的信息.
          如何報表輸出:
          經辦人簽發的證件種類為XX、車輛種類為XX的通行證數XX單,對應的車輛數為XX?  回復  更多評論
            
          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          BlogJava熱點博客

          好友博客

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 连云港市| 进贤县| 柳江县| 丰城市| 洛川县| 卫辉市| 宁津县| 台南县| 昌宁县| 宁海县| 柯坪县| 娱乐| 安图县| 社旗县| 眉山市| 竹北市| 南投县| 曲靖市| 纳雍县| 仲巴县| 仁化县| 怀宁县| 讷河市| 湾仔区| 从江县| 洛宁县| 桐乡市| 达尔| 马山县| 屏南县| 华宁县| 休宁县| 台中市| 平湖市| 灵武市| 肇源县| 安溪县| 宁津县| 师宗县| 城步| 东乌珠穆沁旗|