用SQL語句產生帶有小計合計的資料集指令碼

來源:互聯網
上載者:User

使用SQL語句產生帶有小計合計的資料集:

測試使用者: scott

測試用表: dept,emp

//////////////////////////////////

//檢索出需要進行統計的資料集

select dept.dname,emp.job,sal from emp,dept

where emp.deptno=dept.deptno;

//////////////////////////////////

//根據部門名稱以及職位進行匯總,並為每個部門

產生'小計',最後產生'合計'.

select

decode(grouping(dept.dname),1,'合計:',dept.dname)dname,

decode(grouping(emp.job)+grouping(dept.dname),1,'小計:',emp.job)job,sum(sal) sum_sal from emp,dept where emp.deptno=dept.deptno group by rollup(dept.dname,emp.job);

運行結果如下:

SQL> select dept.dname,emp.job,sal from emp,d

DNAME JOB SAL

-------------- --------- ----------

RESEARCH CLERK 800

SALES SALESMAN 1600

SALES SALESMAN 1250

RESEARCH MANAGER 2975

SALES SALESMAN 1250

SALES MANAGER 2850

ACCOUNTING MANAGER 2450

RESEARCH ANALYST 3000

ACCOUNTING PRESIDENT 5000

SALES SALESMAN 1500

RESEARCH CLERK 1100

DNAME JOB SAL

-------------- --------- ----------

SALES CLERK 950

RESEARCH ANALYST 3000

ACCOUNTING CLERK 1300


已選擇14行。


SQL> select

2 decode(grouping(dept.dname),1,'合計:',de

3 decode(grouping(emp.job)+grouping(dept.d

ept where emp.deptno=dept.deptno group by rol


DNAME JOB SUM_SAL

-------------- --------- ----------

ACCOUNTING CLERK 1300

ACCOUNTING MANAGER 2450

ACCOUNTING PRESIDENT 5000

ACCOUNTING 小計: 8750

RESEARCH ANALYST 6000

RESEARCH CLERK 1900

RESEARCH MANAGER 2975

RESEARCH 小計: 10875

SALES CLERK 950

SALES MANAGER 2850

SALES SALESMAN 5600


DNAME JOB SUM_SAL

-------------- --------- ----------

SALES 小計: 9400

合計: 29025


已選擇13行。


SQL>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.