Oracle PL/SQL之GROUP BY CUBE

來源:互聯網
上載者:User

ROLLUP會根據GROUP BY後面的欄位從右至左逐步以去掉右邊一個欄位,逐步向上累計求和,

CUBE則會對GROUP BY後面的欄位所有的組合進行分組求和。

 

CUBE eg1:

 SELECT department_id, job_id, SUM (salary)<br /> FROM employees<br /> WHERE department_id < 60<br />GROUP BY CUBE (job_id, department_id);</p><p>DEPARTMENT_ID JOB_ID SUM(SALARY)<br />------------- ---------- -----------<br /> 211200<br /> 10 4400<br /> 20 19000<br /> 30 24900<br /> 40 6500<br /> 50 156400<br /> HR_REP 6500<br /> 40 HR_REP 6500<br /> MK_MAN 13000<br /> 20 MK_MAN 13000<br /> MK_REP 6000<br /> 20 MK_REP 6000<br /> PU_MAN 11000<br /> 30 PU_MAN 11000<br /> ST_MAN 36400<br /> 50 ST_MAN 36400<br /> AD_ASST 4400<br /> 10 AD_ASST 4400<br /> PU_CLERK 13900<br /> 30 PU_CLERK 13900<br /> SH_CLERK 64300</p><p>DEPARTMENT_ID JOB_ID SUM(SALARY)<br />------------- ---------- -----------<br /> 50 SH_CLERK 64300<br /> ST_CLERK 55700<br /> 50 ST_CLERK 55700</p><p>24 rows selected.

 

CUBE eg2:

SELECT department_id, job_id, SUM (salary)<br /> FROM employees<br /> WHERE department_id < 60<br />GROUP BY CUBE (department_id, job_id);</p><p>DEPARTMENT_ID JOB_ID SUM(SALARY)<br />------------- ---------- -----------<br /> 211200<br /> HR_REP 6500<br /> MK_MAN 13000<br /> MK_REP 6000<br /> PU_MAN 11000<br /> ST_MAN 36400<br /> AD_ASST 4400<br /> PU_CLERK 13900<br /> SH_CLERK 64300<br /> ST_CLERK 55700<br /> 10 4400<br /> 10 AD_ASST 4400<br /> 20 19000<br /> 20 MK_MAN 13000<br /> 20 MK_REP 6000<br /> 30 24900<br /> 30 PU_MAN 11000<br /> 30 PU_CLERK 13900<br /> 40 6500<br /> 40 HR_REP 6500<br /> 50 156400</p><p>DEPARTMENT_ID JOB_ID SUM(SALARY)<br />------------- ---------- -----------<br /> 50 ST_MAN 36400<br /> 50 SH_CLERK 64300<br /> 50 ST_CLERK 55700</p><p>24 rows selected.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.