Oracle command CUBE

Source: Internet
Author: User

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 ):

  1. SQL>SELECT*FROMEMP;
  2. EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
  3. ------------------------------------------------------------
  4. 7369 smith clerk 7902 800.00 20
  5. 7499 allen salesman 7698 1600.00 30
  6. 7521 ward salesman 7698 1250.00 30
  7. 7566 jones manager 7839 2975.00 20
  8. 7654 martin salesman 7698 1250.00 30
  9. 7698 blake manager 7839 2850.00 30
  10. 7782 clark manager 7839 2450.00 10
  11. 7788 scott analyst 7566 1987/4/19 3000.00 20
  12. 7839 king president 1981/11/17 5000.00 10
  13. 7844 turner salesman 7698 1500.00 30
  14. 7876 adams clerk 7788 1987/5/23 1100.00 20
  15. 7900 james clerk 7698 950.00 30
  16. 7902 ford analyst 7566 3000.00 20
  17. 7934 miller clerk 7782 1982/1/23 1300.00 10

2) calculate the average salary (SAL) of different occupations based on the JOB ):

  1. SQL>SELECTJOB,AVG(SAL)FROMEMPGROUP BYJOBORDER BYJOBASC;
  2. JOBAVG(SAL)
  3. -------------------
  4. Analytic 3000
  5. CLERK 1037.5
  6. Management 2758.33333
  7. PRESIDENT 5000
  8. SALESMAN 1400

3) calculate the average salary (SAL) based on different departments (DEPTNO) and jobs ):

  1. SQL>SELECTJOB, DEPTNO,AVG(SAL)FROMEMPGROUP BYJOB, DEPTNOORDER BYJOB;
  2. JOB DEPTNOAVG(SAL)
  3. -------------------------
  4. ANALYST 20 3000
  5. CLERK 10 1300
  6. CLERK 20 950
  7. CLERK 30 950
  8. MANAGER 10 2450
  9. MANAGER 20 2975
  10. MANAGER 30 2850
  11. PRESIDENT 10 5000
  12. SALESMAN 30 1400

-- We can see that the average annual salary AVG (SAL) of different departments (DEPTNO: 10, 20, 30) is different.

  • 1
  • 2
  • 3
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.