Oracle basic command: group by cube (Field List );
The CUBE command generates different permutation and Combination Based on the field list, and generates statistical summary based on each combination result.
For example: group by cube (city, job, age); shows the result of the permutation and combination:
1) city, job, age
2) city, job
3) city
4) job, age
5) age
6) city, age
7) job
So there are seven types of permutation and combination.
2) generate different statistical summaries based on each sort and combination, as shown below:
1) group by cube (city, job, age );
2) group by cube (city, job );
3) group by cube (city );
4) group by cube (job, age );
5) group by cube (age );
6) group by cube (city, age );
7) group by cube (job );
Example:
1) First, let's look at the tables we will use ):
- SQL>SELECT*FROMEMP;
- EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
- ------------------------------------------------------------
- 7369 smith clerk 7902 800.00 20
- 7499 allen salesman 7698 1600.00 30
- 7521 ward salesman 7698 1250.00 30
- 7566 jones manager 7839 2975.00 20
- 7654 martin salesman 7698 1250.00 30
- 7698 blake manager 7839 2850.00 30
- 7782 clark manager 7839 2450.00 10
- 7788 scott analyst 7566 1987/4/19 3000.00 20
- 7839 king president 1981/11/17 5000.00 10
- 7844 turner salesman 7698 1500.00 30
- 7876 adams clerk 7788 1987/5/23 1100.00 20
- 7900 james clerk 7698 950.00 30
- 7902 ford analyst 7566 3000.00 20
- 7934 miller clerk 7782 1982/1/23 1300.00 10
2) calculate the average salary (SAL) of different occupations based on the JOB ):
- SQL>SELECTJOB,AVG(SAL)FROMEMPGROUP BYJOBORDER BYJOBASC;
- JOBAVG(SAL)
- -------------------
- Analytic 3000
- CLERK 1037.5
- Management 2758.33333
- PRESIDENT 5000
- SALESMAN 1400
3) calculate the average salary (SAL) based on different departments (DEPTNO) and jobs ):
- SQL>SELECTJOB, DEPTNO,AVG(SAL)FROMEMPGROUP BYJOB, DEPTNOORDER BYJOB;
- JOB DEPTNOAVG(SAL)
- -------------------------
- ANALYST 20 3000
- CLERK 10 1300
- CLERK 20 950
- CLERK 30 950
- MANAGER 10 2450
- MANAGER 20 2975
- MANAGER 30 2850
- PRESIDENT 10 5000
- SALESMAN 30 1400
-- We can see that the average annual salary AVG (SAL) of different departments (DEPTNO: 10, 20, 30) is different.