ROLLUP literally means to roll to, used in group by can play a cumulative sum role:
If no ROLLUP exists, the following query sums the groups by department_id and job_id:
- SELECT department_id, job_id, SUM (salary)
- FROM employees
- WHERE department_id <60
- Group by department_id, job_id;
Output:
- DEPARTMENT_ID, JOB_ID, SUM (SALARY)
- 50, ST_CLERK, 55700
- 50, ST_MAN, 36400
- 30, PU_CLERK, 13900
- 50, SH_CLERK, 64300
- 20, MK_MAN, 13000
- Thirty, PU_MAN, 11000
- 10, AD_ASST, 4400
- 20, MK_REP, 6000
- 40, HR_REP, 6500
With ROLLUP:
Group and sum department_id and job_id, sum them according to department_id, and finally calculate the sum:
- SELECT department_id, job_id, SUM (salary)
- FROM employees
- WHERE department_id <60
- Group by rollup (department_id, job_id );
Output:
- DEPARTMENT_ID, JOB_ID, SUM (SALARY)
- 10, AD_ASST, 4400
- 10, 4400
- 20, MK_MAN, 13000
- 20, MK_REP, 6000
- 20, 19000
- Thirty, PU_MAN, 11000
- 30, PU_CLERK, 13900
- 30, 24900
- 40, HR_REP, 6500
- 40, 6500
- 50, ST_MAN, 36400
- 50, SH_CLERK, 64300
- 50, ST_CLERK, 55700
- 50, 156400
- , 211200
Group and sum the department_id and job_id, sum the sum according to job_id, and finally calculate the sum:
- SELECT department_id, job_id, SUM (salary)
- FROM employees
- WHERE department_id <60
- Group by rollup (job_id, department_id );
Output:
- DEPARTMENT_ID, JOB_ID, SUM (SALARY)
- 40, HR_REP, 6500
- HR_REP, 6500
- 20, MK_MAN, 13000
- , MK_MAN, 13000
- 20, MK_REP, 6000
- , MK_REP 6000
- Thirty, PU_MAN, 11000
- , PU_MAN, 11000
- 50, ST_MAN, 36400
- , ST_MAN, 36400
- 10, AD_ASST, 4400
- AD_ASST, 4400
- 30, PU_CLERK, 13900
- , PU_CLERK, 13900
- 50, SH_CLERK, 64300
- , SH_CLERK, 64300
- 50, ST_CLERK, 55700
- , ST_CLERK, 55700
- , 211200