Oracle PL/SQL GROUP BY CUBE

Source: Internet
Author: User

ROLLUP will gradually remove a field on the right from the right to the left Based on the field following group by, and gradually sum up,

CUBE sums all the combinations of fields after group.

CUBE eg1:

  1. SELECT department_id, job_id, SUM (salary)
  2. FROM employees
  3. WHERE department_id <60
  4. Group by cube (job_id, department_id );
  5. DEPARTMENT_ID JOB_ID SUM (SALARY)
  6. ----------------------------------
  7. 211200
  8. 10 4400
  9. 20 19000
  10. 30 24900
  11. 40 6500
  12. 50 156400
  13. HR_REP 6500
  14. 40 HR_REP 6500
  15. MK_MAN 13000
  16. 20 MK_MAN 13000
  17. MK_REP 6000
  18. 20 MK_REP 6000
  19. PU_MAN 11000
  20. 30 PU_MAN 11000
  21. ST_MAN 36400
  22. 50 ST_MAN 36400
  23. AD_ASST 4400
  24. 10 AD_ASST 4400
  25. PU_CLERK 13900
  26. 30 PU_CLERK 13900
  27. SH_CLERK 64300
  28. DEPARTMENT_ID JOB_ID SUM (SALARY)
  29. ----------------------------------
  30. 50 SH_CLERK 64300
  31. ST_CLERK 55700
  32. 50 ST_CLERK 55700
  33. 24 rows selected.

CUBE eg2:

  1. SELECT department_id, job_id, SUM (salary)
  2. FROM employees
  3. WHERE department_id <60
  4. Group by cube (department_id, job_id );
  5. DEPARTMENT_ID JOB_ID SUM (SALARY)
  6. ----------------------------------
  7. 211200
  8. HR_REP 6500
  9. MK_MAN 13000
  10. MK_REP 6000
  11. PU_MAN 11000
  12. ST_MAN 36400
  13. AD_ASST 4400
  14. PU_CLERK 13900
  15. SH_CLERK 64300
  16. ST_CLERK 55700
  17. 10 4400
  18. 10 AD_ASST 4400
  19. 20 19000
  20. 20 MK_MAN 13000
  21. 20 MK_REP 6000
  22. 30 24900
  23. 30 PU_MAN 11000
  24. 30 PU_CLERK 13900
  25. 40 6500
  26. 40 HR_REP 6500
  27. 50 156400
  28. DEPARTMENT_ID JOB_ID SUM (SALARY)
  29. ----------------------------------
  30. 50 ST_MAN 36400
  31. 50 SH_CLERK 64300
  32. 50 ST_CLERK 55700
  33. 24 rows selected.

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.