SQL之17深入group by
ROLLUP在GROUP BY中的使用
SELECT department_id, job_id,SUM(salary)
FROM employees
WHERE department_id < 60
GROUP BY ROLLUP(department_id, job_id);
DEPTNO JOB AVG(SAL)
------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 2916.66667
20 CLERK 950
20 ANALYST 3000
20 MANAGER 2975
20 1968.75
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 1400
30 1566.66667
40
40
50
50 ANALYST 3000
50 3000
2073.21429
結果:除了分組外,還加上按DEPTNO的小計
cube在GROUP BY中的使用
SQL> /
DEPTNO JOB SUM(SAL)
---------- --------- ----------
29025
CLERK 4150
ANALYST 6000
MANAGER 8275
SALESMAN 5600
PRESIDENT 5000
10 8750
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 7875
20 CLERK 1900
20 ANALYST 3000
20 MANAGER 2975
30 9400
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
40
40
50
50 3000
50 ANALYST 3000
24 rows selected.
SQL> ;
1 select deptno,job,sum(sal)
2* from emp group by cube(deptno,job)
SQL>
結果:除了分組外,按DEPTNO的小計,並按job進行sum(sal),顯示在行集前面.
Grouping 結合cube或rollup在GROUP BY中的使用
SQL> ;
1 select deptno,job,sum(sal),
2 grouping(deptno) grp_dept,grouping(job) grp_job
3* from emp where deptno<60 group by cube(deptno,job)
SQL> /
DEPTNO JOB SUM(SAL) GRP_DEPT GRP_JOB
---------- --------- ---------- ---------- ----------
1 0
29025 1 1
CLERK 4150 1 0
ANALYST 6000 1 0
MANAGER 8275 1 0
SALESMAN 5600 1 0
PRESIDENT 5000 1 0
10 8750 0 1
10 CLERK 1300 0 0
10 MANAGER 2450 0 0
10 PRESIDENT 5000 0 0
20 7875 0 1
20 CLERK 1900 0 0
20 ANALYST 3000 0 0
20 MANAGER 2975 0 0
30 9400 0 1
30 CLERK 950 0 0
30 MANAGER 2850 0 0
30 SALESMAN 5600 0 0
40 0 0
40 0 1
50 0 0
50 3000 0 1
50 ANALYST 3000 0 0
24 rows selected.
Grouping sets 代替cube或rollup在GROUP BY中的使用
SQL> select deptno,job,mgr,sum(sal)
2 from emp group by grouping sets
3 ((deptno,job),(job,mgr));
DEPTNO JOB MGR SUM(SAL)
---------- --------- ---------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 CLERK 1900
20 ANALYST 3000
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
40
50
50 ANALYST 3000
CLERK 7698 950
CLERK 7782 1300
CLERK 7788 1100
CLERK 7902 800
ANALYST 7566 6000
MANAGER 7839 8275
SALESMAN 7698 5600
PRESIDENT 5000
21 rows selected.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-909523/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL之19深入group bySQL
- SQL之18深入group bySQL
- Oracle PL/SQL之GROUP BY CUBEOracleSQL
- 深入理解GCD之dispatch_groupGC
- 深入淺出SQL教程之Group by和Having(轉貼)SQL
- 深入SQL之merge intoSQL
- sql用法——group bySQL
- SQL入門之4 group by 與子查詢SQL
- 深入SQL之 insert allSQL
- SQL Server之深入理解STUFFSQLServer
- SQL中Group By的使用SQL
- SQL 分組排序group bySQL排序
- (17)sql注入與sql modeSQL
- [深入17] HTTP 和 HTTPSHTTP
- PostgreSQL DBA(186) - SQL Group BySQL
- sql case when, Exist ,group by ,聚合SQL
- 兩層GROUP BY效率反而比一層GROUP BY效率高——SQL優化之Everything is possibleSQL優化
- three.js之GroupJS
- SQL函式Group_concat用法SQL函式
- mysql sql_mode ONLY_FULL_GROUP_BYMySql
- SQL -去重Group by 和Distinct的效率SQL
- oracle全文索引之SECTION GROUP_6_PATH_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_5_AUTO_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_4_XML_SECTION_GROUPOracle索引XML
- oracle全文索引之SECTION GROUP_3_HTML_SECTION_GROUPOracle索引HTML
- oracle全文索引之SECTION GROUP_2_BASIC_SECTION_GROUPOracle索引
- oracle全文索引之SECTION GROUP_1_NULL_SECTION_GROUPOracle索引Null
- 【SQL】17 SQL 檢視(Views)、SQL Date 函式、SQL NULL 值、SQLView函式Null
- MySQL 之 only_full_group_byMySql
- 17_深入解析Oracle undo原理(1)_transactionOracle
- SQL Server Availability Group Failover 測試SQLServerAI
- SQL改寫的方法,select group by sumSQL
- 【學習】SQL基礎-005-group by子句SQL
- SQL 2012 Group By Rollup, GroupingSQL
- sql中的group by 和 having 用法解析SQL
- dedecms /member/myfriend_group.php SQL Injection VulPHPSQL
- java正規表示式之 groupJava
- 【恩墨學院】深入剖析 Group Replication核心的引擎特性