擴(kuò)展GROUP BY概述
Oracle擴(kuò)展GROUP BY允許使用SQL語(yǔ)句對(duì)數(shù)據(jù)匯總結(jié)果進(jìn)行多維展現(xiàn),從而生成復(fù)雜的報(bào)表,為決策者提供有效的數(shù)據(jù)支持。主要表現(xiàn)在:
通過(guò)SQL語(yǔ)句對(duì)上述功能的組合使用,就可以實(shí)現(xiàn)制作復(fù)雜的多維分析報(bào)表的功能。針對(duì)不同維度的報(bào)表統(tǒng)計(jì),使用擴(kuò)展GROUP BY的強(qiáng)大功能很容易實(shí)現(xiàn),而且SQL編寫(xiě)更簡(jiǎn)單,性能也比同等的UNION ALL更好,在后面的內(nèi)容中,我們會(huì)見(jiàn)識(shí)到強(qiáng)大的擴(kuò)展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行。
執(zhí)行計(jì)劃
----------------------------------------------------------
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對(duì)兩列操作,對(duì)應(yīng)4個(gè)分組級(jí)別,最終對(duì)各種可能性分組進(jìn)行統(tǒng)計(jì),獲得多維度更加精細(xì)的數(shù)據(jù)統(tǒng)計(jì)結(jié)果。
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一樣,可以實(shí)現(xiàn)多維數(shù)組分析統(tǒng)計(jì)工作,而且CUBE是對(duì)所有可能性的組合情況進(jìn)行統(tǒng)計(jì),從而生成交叉報(bào)表,CUBE分組級(jí)別更多,結(jié)果更精細(xì),從而為決策者提供強(qiáng)大的數(shù)據(jù)支撐,為實(shí)現(xiàn)靈活的報(bào)表提供保障。
GROUPING SETS實(shí)現(xiàn)小計(jì)
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行。
執(zhí)行計(jì)劃
----------------------------------------------------------------------------------------------
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);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/129100.html
摘要:當(dāng)向傳入一列時(shí),會(huì)得到一個(gè)總計(jì)行。結(jié)果當(dāng)向傳遞兩列時(shí),將會(huì)按照這兩列進(jìn)行分組,同時(shí)按照第一列的分組結(jié)果返回小計(jì)行。結(jié)果可以看出來(lái)結(jié)果是按照工廠和部門分別分組匯總的。選擇的就表示兩列都不為空。 Group By Group By 誰(shuí)不會(huì)啊?這不是最簡(jiǎn)單的嗎?越是簡(jiǎn)單的東西,我們?cè)綍?huì)忽略掉他,因?yàn)槲覀儾辉敢庠偃ド钊肓私馑? 小時(shí) SQL 極速入門(一)1 小時(shí) SQL 極速入門(二)1 ...
閱讀 1345·2023-01-11 13:20
閱讀 1680·2023-01-11 13:20
閱讀 1132·2023-01-11 13:20
閱讀 1857·2023-01-11 13:20
閱讀 4098·2023-01-11 13:20
閱讀 2704·2023-01-11 13:20
閱讀 1385·2023-01-11 13:20
閱讀 3594·2023-01-11 13:20