[Developer] Grouping sets
grouping sets
group by grouping sets(a,b,c) = group by a
union all group by b
union all group by c
SQL> list
1 select to_char(hiredate,'yyyy'),deptno,job,sum(sal)
2 from emp
3* group by grouping sets(to_char(hiredate,'yyyy'),deptno,job)
SQL> /
TO_CHAR(HIRE DEPTNO JOB SUM(SAL)
------------ ---------- --------------------------- ----------
CLERK 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 6000
30 9400
20 10875
10 8750
1987 4100
1980 800
1982 1300
1981 22825
12 rows selected.
將to_char(hiredate,'yyyy')移到group中,
對於每個入職年份, 每個部門+每個職位的統計 和 每個職位+每個部門 的統計。
SQL> edit
Wrote file afiedt.buf
1 select to_char(hiredate,'yyyy'),deptno,job,sum(sal)
2 from emp
3 group by to_char(hiredate,'yyyy'),grouping sets(deptno,job)
4* order by 1
SQL> /
TO_CHAR(HIRE DEPTNO JOB SUM(SAL)
------------ ---------- --------------------------- ----------
1980 20 800
1980 CLERK 800
1981 SALESMAN 5600
1981 ANALYST 3000
1981 MANAGER 8275
1981 30 9400
1981 PRESIDENT 5000
1981 CLERK 950
1981 20 5975
1981 10 7450
1982 10 1300
1982 CLERK 1300
1987 ANALYST 3000
1987 20 4100
1987 CLERK 1100
15 rows selected.
grouping sets中加入rollup
SQL> edit
Wrote file afiedt.buf
1 select deptno,job,sum(sal)
2 from emp
3* group by grouping sets(rollup(deptno),rollup(job))
SQL> /
DEPTNO JOB SUM(SAL)
---------- --------------------------- ----------
CLERK 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 6000
10 8750
20 10875
30 9400
29025
29025
10 rows selected.
rollup,cube不能接受grouping sets作為引數,rollup和cube之間也不能相互作為引數。
group by grouping sets(a,b,c) = group by a
union all group by b
union all group by c
SQL> list
1 select to_char(hiredate,'yyyy'),deptno,job,sum(sal)
2 from emp
3* group by grouping sets(to_char(hiredate,'yyyy'),deptno,job)
SQL> /
TO_CHAR(HIRE DEPTNO JOB SUM(SAL)
------------ ---------- --------------------------- ----------
CLERK 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 6000
30 9400
20 10875
10 8750
1987 4100
1980 800
1982 1300
1981 22825
12 rows selected.
將to_char(hiredate,'yyyy')移到group中,
對於每個入職年份, 每個部門+每個職位的統計 和 每個職位+每個部門 的統計。
SQL> edit
Wrote file afiedt.buf
1 select to_char(hiredate,'yyyy'),deptno,job,sum(sal)
2 from emp
3 group by to_char(hiredate,'yyyy'),grouping sets(deptno,job)
4* order by 1
SQL> /
TO_CHAR(HIRE DEPTNO JOB SUM(SAL)
------------ ---------- --------------------------- ----------
1980 20 800
1980 CLERK 800
1981 SALESMAN 5600
1981 ANALYST 3000
1981 MANAGER 8275
1981 30 9400
1981 PRESIDENT 5000
1981 CLERK 950
1981 20 5975
1981 10 7450
1982 10 1300
1982 CLERK 1300
1987 ANALYST 3000
1987 20 4100
1987 CLERK 1100
15 rows selected.
grouping sets中加入rollup
SQL> edit
Wrote file afiedt.buf
1 select deptno,job,sum(sal)
2 from emp
3* group by grouping sets(rollup(deptno),rollup(job))
SQL> /
DEPTNO JOB SUM(SAL)
---------- --------------------------- ----------
CLERK 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 6000
10 8750
20 10875
30 9400
29025
29025
10 rows selected.
rollup,cube不能接受grouping sets作為引數,rollup和cube之間也不能相互作為引數。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24237320/viewspace-2081212/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL grouping sets 子句SQL
- 分析函式 - GROUPING SETS函式
- GROUP BY中ROLLUP/CUBE/GROUPING/GROUPING SETS使用示例
- Oracle OCP(13):GROUPING & GROUPING_ID & GROUP_ID & GROUPING SETSOracle
- 關於 grouping sets 學習
- 分析函式 - CUBE和GROUPING SETS函式
- 分析函式 - ROLLUP和GROUPING SETS函式
- Oracle的rollup、cube、grouping sets函式Oracle函式
- 解析數倉OLAP函式:ROLLUP、CUBE、GROUPING SETS函式
- SQL groupingSQL
- Grouping函式使用函式
- Equivalent Sets(HDU-3836)UI
- mongodb replica sets 測試MongoDB
- 分析函式 - GROUPING_ID函式
- GROUPING_ID的應用
- Using Sorted Sets with Jedis APIAPI
- 上海聘:SCM Java Developer/Senior DeveloperJavaDeveloper
- MongoDB 複製集模式Replica SetsMongoDB模式
- Replica sets複製集的搭建
- [轉]Trees in SQL: Nested Sets and Materialized PathSQLZed
- Result Sets from Stored Procedures In Oracle (轉)Oracle
- pl developerDeveloper
- [Developer] CubeDeveloper
- [Developer] RollupDeveloper
- SQL 2012 Group By Rollup, GroupingSQL
- Simple Automated Backups for MongoDB Replica SetsMongoDB
- Three Sets of Vacuum Degassers Awaiting ShipmentAI
- PLSQL Developer 14SQLDeveloper
- group by中rollup的以及grouping含義解析
- MongoDB系列二:Replica Sets安裝與配置MongoDB
- R語言中的迴圈函式(Grouping Function)R語言函式Function
- PLSQL Developer 行號SQLDeveloper
- PLSQL Developer配置使用SQLDeveloper
- Developer Zone Android*DeveloperAndroid
- PL/SQl Developer使用SQLDeveloper
- rabbit Clients & Developer ToolsclientDeveloper
- mozilla css developer centerCSSDeveloper
- PL/SQL Developer 使用SQLDeveloper