使用Oracle 10g引入的Partition Outer Joins進行統計報表的實現
SELECT * FROM employee_expense;
EMP_ID YEAR MONTH EXPENSE_CLAIM APPROVED_AMT PAID_DATE
---------- ---------- ---------- ------------- ------------ ---------
7369 2002 2 3072.43 3072.43 03-MAR-02
7369 2002 4 30 30 01-JUN-02
7369 2002 5 235.03 35.03 01-JUN-02
7369 2002 9 5095.98 5095.08 31-OCT-02
7369 2002 12 1001.01 1001.01 01-FEB-03
7782 2002 1 111.09 111.09 01-FEB-02
7782 2002 3 9.85 9.85 01-APR-02
7782 2002 7 3987.32 3987.32 01-AUG-02
7782 2002 9 1200 1200 01-OCT-02
SELECT * FROM months WHERE year = 2002; YEAR MONTH ---------- ---------- 2002 1 2002 2 2002 3 2002 4 2002 5 2002 6 2002 7 2002 8 2002 9 2002 10 2002 11 2002 12
SELECT ee.emp_id, m.year, m.month, NVL(ee.expense_claim,0) FROM (SELECT * FROM months WHERE year = 2002) m LEFT OUTER JOIN employee_expense ee PARTITION BY (ee.emp_id) ON m.year = ee.year AND m.month = ee.month ORDER BY ee.emp_id, m.month; EMP_ID YEAR MONTH NVL(EE.EXPENSE_CLAIM,0) ---------- ---------- ---------- ----------------------- 7369 2002 1 0 7369 2002 2 3072.43 7369 2002 3 0 7369 2002 4 30 7369 2002 5 235.03 7369 2002 6 0 7369 2002 7 0 7369 2002 8 0 7369 2002 9 5095.98 7369 2002 10 0 7369 2002 11 0 7369 2002 12 1001.01 7782 2002 1 111.09 7782 2002 2 0 7782 2002 3 9.85 7782 2002 4 0 7782 2002 5 0 7782 2002 6 0 7782 2002 7 3987.32 7782 2002 8 0 7782 2002 9 1200 7782 2002 10 0 7782 2002 11 0 7782 2002 12 0
Notice the PARTITION BY clause in this query. That clause is new in Oracle Database 10g, and in this example it causes the database engine to conceptually perform the following steps:
-
Divide the rows from employee_expense into groups based on their emp_id values, one group per value.
-
Outer join each group to the months table as a separate operation.
The key here is that rather than one outer join, you are getting the equivalent of many outer joins, but with a much simpler syntax, and from one query. The preceding query is logically equivalent to the following UNION ALL query:
SELECT NVL(ee.emp_id, 7369), m.year, m.month, NVL(ee.expense_claim,0) FROM (SELECT * FROM months WHERE year = 2002) m LEFT OUTER JOIN (SELECT * FROM employee_expense WHERE emp_id = 7369) ee ON m.year = ee.year AND m.month = ee.month ORDER BY m.month UNION ALL SELECT NVL(ee.emp_id, 7782), m.year, m.month, NVL(ee.expense_claim,0) FROM (SELECT * FROM months WHERE year = 2002) m LEFT OUTER JOIN (SELECT * FROM employee_expense WHERE emp_id = 7782) ee ON m.year = ee.year AND m.month = ee.month ORDER BY m.month;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26521853/viewspace-1255289/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用Partitioned Outer Join實現稠化報表
- 【執行計劃】Oracle 11gR2使用Full outer Joins執行計劃完成全外連線查詢Oracle
- Lerning Entity Framework 6 ------ Joins and Left outer JoinsFramework
- Partition-wise Joins
- Partition Pruning和Partition-Wise Joins
- 如何實現報表滾動到底部進行翻頁的效果
- 報表合計需求的實現方法
- oracle中通過decode實現行變列的二維表統計展示Oracle
- oracle統計表的所有行數(原創)Oracle
- Oracle JoinsOracle
- Oracle Interval Partition 自動分割槽表-實驗Oracle
- Oracle實現統計Oracle
- oracle 10g的自動統計分析Oracle 10g
- Oracle分割槽表(Partition Table)Oracle
- Oracle統計分析函式集,over(partition by..) 的運用Oracle函式
- 報表如何實現對資料列進行排名分析?
- 使用 Oracle Database 10g中的閃回表特性OracleDatabase
- oracle 10g表沒有收集統計資訊的一點研究與思考Oracle 10g
- 潤乾報表中進度條的一種實現方式
- oracle partition的方法Oracle
- 22New Cascalog features: outer joins, combiners, sorting, and more
- 報表系統的設計要素
- 實現報表與演算法的統一管理演算法
- 分享一個開源的JavaScript統計圖表庫,40行程式碼實現專業統計圖表JavaScript行程
- 使用GoldenGate 實現Oracle for Oracle 單向資料同步(實現表的DML操作同步)GoOracle
- 在VB6.0中實現動態統計報表 (轉)
- 【實驗】【PARTITION】RANGE分割槽表截斷表分割槽(Truncate Partition)
- 【實驗】【PARTITION】RANGE分割槽表移動表分割槽(Move Partition)
- [20171002]NESTED LOOPS(PARTITION OUTER).TXTOOP
- MySQL表與表之間的SQL Joins圖介紹MySql
- 用left outer join(sql)實現只顯示重複行最小id的記錄SQL
- [MySQL] 行列變化各種方法實現總結(行變列報表統計、列變行資料記錄統計等)MySql
- ACCESS 統計報表有多少行記錄
- [總結] Oracle表的分析統計Oracle
- 使用exchange partition來交換不同schema之間的表
- 如何實現報表的批次列印需求
- oracle 10g awr報告的收集Oracle 10g
- Angular8 引入百度 Echarts,進行圖表分析AngularEcharts