Download presentation
Presentation is loading. Please wait.
Published byMoses Neal Modified over 9 years ago
1
ORACLE SQL Fundamental II xpp-e-f 重點 xpp-e : Generating Reports by Grouping Related Data xpp-f : Hierarchical Retrieval
2
II e-2 Objectives ROLLUP operation to produce subtotal values( 小計 ) CUBE operation to produce cross-tabulation values( 跨表 格值 ) GROUPING function to identify the row values created by ROLLUP or CUBE GROUPING SETS to produce a single result set 2 題庫 125
3
II e-2 GROUP BY with ROLLUP and CUBE Operators Use ROLLUP or CUBE with GROUP BY : produce superaggregate rows by cross-referencing columns ROLLUP grouping : produces a result set containing the regular grouped rows and the subtotal values CUBE grouping : produces a result set containing the rows from ROLLUP and cross-tabulation rows 3 題庫 2
4
II e-8 ROLLUP Operator: Example ROLLUP :對第一個分組欄位加總 SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY ROLLUP(department_id, job_id) ; 4 題庫 93
5
II e-10 CUBE Operator: Example CUBE : 對所有分組欄位加總 SELECT department_id, job_id, SUM(salary) FROM employees WHERE department_id < 60 GROUP BY CUBE (department_id, job_id) ; 5 題庫 14 題庫 88 題庫 113
6
II e-12 GROUPING Function: Example SELECT department_id DEPTID, job_id JOB, SUM(salary), GROUPING(department_id) GRP_DEPT, GROUPING(job_id) GRP_JOB FROM employees WHERE department_id < 50 GROUP BY ROLLUP(department_id, job_id) ; 6 GRP_DEPT 及 GRP_JOB 為 1 者 : 表示小計 題庫 125
7
II e-15 GROUPING SETS: Example SELECT department_id, job_id, manager_id, AVG(salary) FROM employees GROUP BY GROUPING SETS ((department_id,job_id), (job_id,manager_id)) ; 7
8
II e-19 Composite Columns: Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY ROLLUP( department_id,(job_id, manager_id)) ; 8
9
II e-22 Concatenated Groupings: Example SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY department_id, ROLLUP(job_id), CUBE(manager_id) ; 9
10
補充 CUBE & ROLLUP CUBE (a,b,c) GROUPING SETS//2 的 3 次方 is equivalent to ((a,b,c),(a,b),(a,c),(b,c),(a),(b),(c)()) ROLLUP(a,b,c)GROUPING SETS is equivalent to((a,b,c),(a,b),(a),()) 參考 : http://blog.chinaunix.net/uid-17277885-id-2809624.html http://wupinny.blogspot.tw/2009/11/oraclegroup-by-rollup- function.html 10
11
II f-2 Objectives Interpret the concept of a hierarchical query Create a tree-structured report Format hierarchical data Exclude branches from the tree structure 11
12
II f-3&f4 Natural Tree Structure EMPLOYEES Table 12 Natural Tree Structure
13
II f-5 Hierarchical Queries SELECT [LEVEL], column, expr... FROM table [WHERE condition(s)] [START WITH condition(s)] [CONNECT BY PRIOR condition(s)] ; 13 題庫 4
14
II f-6 Walking the Tree START WITH column1 = value 例 : 使用 EMPLOYEES 資料表, 從 (start with) 員工 last name 為 Kochhar 開始...START WITH last_name = 'Kochhar' 14
15
II f-7 Walking the Tree CONNECT BY PRIOR column1 = column2 例 : 使用 EMPLOYEES 資料表, Walk from the top down... CONNECT BY PRIOR employee_id = manager_id Direction : 15
16
II f-8 Walking the Tree: From the Bottom Up SELECT employee_id, last_name, job_id, manager_id FROM employees START WITH employee_id = 101 CONNECT BY PRIOR manager_id = employee_id ; 16 Child KeyParent Key
17
II f-9 Walking the Tree: From the Top Down SELECT last_name||' reports to '|| PRIOR last_name "Walk Top Down" FROM employees START WITH last_name = 'King' CONNECT BY PRIOR employee_id = manager_id ; 17 Child Key Parent Key 題庫 63 題庫 74
18
II f-10 Ranking Rows with the LEVEL Pseudocolumn 18
19
II f-11 Formatting Hierarchical Reports by Using LEVEL and LPAD Create a report displaying company management levels, beginning with the highest level and indenting each of the following levels. COLUMN org_chart FORMAT A12 SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS org_chart FROM employees START WITH first_name='Steven' AND last_name='King' CONNECT BY PRIOR employee_id=manager_id 19
20
II f-13 Pruning( 刪除 ) Branches 20 題庫 45
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.