擴展GROUP BY概述
Oracle擴展GROUP BY允許使用SQL語句對數據匯總結果進行多維展現,從而生成復雜的報表,為決策者提供有效的數據支持。主要表現在:
通過SQL語句對上述功能的組合使用,就可以實現制作復雜的多維分析報表的功能。針對不同維度的報表統計,使用擴展GROUP BY的強大功能很容易實現,而且SQL編寫更簡單,性能也比同等的UNION ALL更好,在后面的內容中,我們會見識到強大的擴展GROUP BY功能。
CUBE
C0n + C1n + C2n + … + Cnn = 2n
SELECT … GROUP BY CUBE(grouping_column_reference_list)
SELECT a.dname,b.job, SUM(b.sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY CUBE(a.dname,b.job);
DNAME JOB SUM_SAL
---------------------------- ------------------ ----------
24925
CLERK 3050
ANALYST 3000
MANAGER 8275
SALESMAN 5600
PRESIDENT 5000
SALES 9400
SALES CLERK 950
SALES MANAGER 2850
SALES SALESMAN 5600
RESEARCH 6775
RESEARCH CLERK 800
RESEARCH ANALYST 3000
RESEARCH MANAGER 2975
ACCOUNTING 8750
ACCOUNTING CLERK 1300
ACCOUNTING MANAGER 2450
ACCOUNTING PRESIDENT 5000
已選擇18行。
執行計劃
----------------------------------------------------------
Plan hash value: 2432972551
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 336 | 5 (20)| 00:00:01 |
| 1 | SORT GROUP BY | | 12 | 336 | 5 (20)| 00:00:01 |
| 2 | GENERATE CUBE | | 12 | 336 | 5 (20)| 00:00:01 |
| 3 | SORT GROUP BY | | 12 | 336 | 5 (20)| 00:00:01 |
| 4 | NESTED LOOPS | | 12 | 336 | 4 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | EMP | 12 | 180 | 3 (0)| 00:00:01 |
| 6 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 13 | 1 (0)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
7 - access("A"."DEPTNO"="B"."DEPTNO")
CUBE對兩列操作,對應4個分組級別,最終對各種可能性分組進行統計,獲得多維度更加精細的數據統計結果。
SELECT a.dname,b.job, SUM(b.sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY a.dname,CUBE(b.job);
DNAME JOB SUM_SAL
---------------------------- ------------------ ----------
SALES 9400
SALES CLERK 950
SALES MANAGER 2850
SALES SALESMAN 5600
RESEARCH 6775
RESEARCH CLERK 800
RESEARCH ANALYST 3000
RESEARCH MANAGER 2975
ACCOUNTING 8750
ACCOUNTING CLERK 1300
ACCOUNTING MANAGER 2450
ACCOUNTING PRESIDENT 5000
已選擇12行。
CUBE像ROLLUP一樣,可以實現多維數組分析統計工作,而且CUBE是對所有可能性的組合情況進行統計,從而生成交叉報表,CUBE分組級別更多,結果更精細,從而為決策者提供強大的數據支撐,為實現靈活的報表提供保障。
GROUPING SETS實現小計
SELECT … GROUP BY GROUPING SETS(grouping_column_reference_list)
SELECT to_char(b.hiredate,yyyy) hire_year,a.dname,b.job, SUM(sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY GROUPING SETS(to_char(b.hiredate,yyyy),a.dname,b.job);
HIRE_YEA DNAME JOB SUM_SAL
-------- ---------------------------- ------------------ ----------
CLERK 3050
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 3000
ACCOUNTING 8750
RESEARCH 6775
SALES 9400
1980 800
1982 1300
1981 22825
已選擇11行。
執行計劃
----------------------------------------------------------------------------------------------
Plan hash value: 18386332
-------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 12 | 384 | 15 (20)| 00:00:01 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | | | | | |
| 3 | NESTED LOOPS | | 12 | 432 | 4 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL | EMP | 12 | 276 | 3 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 13 | 1 (0)| 00:00:01 |
|* 6 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |
| 7 | LOAD AS SELECT | | | | | |
| 8 | HASH GROUP BY | | 1 | 19 | 3 (34)| 00:00:01 |
| 9 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6612_4AC9B4F | 1 | 19 | 2 (0)| 00:00:01 |
| 10 | LOAD AS SELECT | | | | | |
| 11 | HASH GROUP BY | | 1 | 22 | 3 (34)| 00:00:01 |
| 12 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6612_4AC9B4F | 1 | 22 | 2 (0)| 00:00:01 |
| 13 | LOAD AS SELECT | | | | | |
| 14 | HASH GROUP BY | | 1 | 17 | 3 (34)| 00:00:01 |
| 15 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6612_4AC9B4F | 1 | 17 | 2 (0)| 00:00:01 |
| 16 | VIEW | | 1 | 32 | 2 (0)| 00:00:01 |
| 17 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6613_4AC9B4F | 1 | 32 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("SYS_TBL_$2$"."DEPTNO"="SYS_TBL_$1$"."DEPTNO")
SELECT a.dname,to_char(b.hiredate,yyyy) hire_year,b.job, SUM(sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY a.dname, GROUPING SETS(to_char(b.hiredate,yyyy),b.job);
DNAME HIRE_YEA JOB SUM_SAL
---------------------------- -------- ------------------ ----------
SALES MANAGER 2850
SALES CLERK 950
ACCOUNTING MANAGER 2450
ACCOUNTING PRESIDENT 5000
ACCOUNTING CLERK 1300
SALES SALESMAN 5600
RESEARCH MANAGER 2975
RESEARCH ANALYST 3000
RESEARCH CLERK 800
RESEARCH 1981 5975
SALES 1981 9400
ACCOUNTING 1981 7450
ACCOUNTING 1982 1300
RESEARCH 1980 800
已選擇14行。
SELECT a.dname,b.job, SUM(sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY GROUPING SETS(ROLLUP(a.dname),ROLLUP(b.job));
DNAME JOB SUM_SAL
---------------------------- ------------------ ----------
CLERK 3050
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 3000
ACCOUNTING 8750
RESEARCH 6775
SALES 9400
24925
24925
SELECT a.dname,NULL job, SUM(sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY ROLLUP(a.dname)
UNION ALL
SELECT NULL dname,b.job, SUM(sal) sum_sal
FROM dept a,emp b
WHERE a.deptno = b.deptno
GROUP BY ROLLUP(b.job);
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/129100.html
摘要:當向傳入一列時,會得到一個總計行。結果當向傳遞兩列時,將會按照這兩列進行分組,同時按照第一列的分組結果返回小計行。結果可以看出來結果是按照工廠和部門分別分組匯總的。選擇的就表示兩列都不為空。 Group By Group By 誰不會啊?這不是最簡單的嗎?越是簡單的東西,我們越會忽略掉他,因為我們不愿意再去深入了解它。1 小時 SQL 極速入門(一)1 小時 SQL 極速入門(二)1 ...
閱讀 1353·2023-01-11 13:20
閱讀 1699·2023-01-11 13:20
閱讀 1211·2023-01-11 13:20
閱讀 1901·2023-01-11 13:20
閱讀 4161·2023-01-11 13:20
閱讀 2747·2023-01-11 13:20
閱讀 1397·2023-01-11 13:20
閱讀 3664·2023-01-11 13:20