1.首先建立一個表
Sql代碼
[sql] view plain copy print ? create table TE ( ID VARCHAR2(2), T_CODE VARCHAR2(4), T_NAME VARCHAR2(4), T_AMOUNT INTEGER, T_DEPT VARCHAR2(4), T_PROJECT VARCHAR2(4), T_TYPE VARCHAR2(1) )
create table TE( ID VARCHAR2(2), T_CODE VARCHAR2(4), T_NAME VARCHAR2(4), T_AMOUNT INTEGER, T_DEPT VARCHAR2(4), T_PROJECT VARCHAR2(4), T_TYPE VARCHAR2(1))
2.錄入資料如下:
Sql代碼
[sql] view plain copy print ? insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('1', '1', '1', 10, '總部', '90', '0'); insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('2', '2', '2', 20, '總部', '70', '0'); insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('3', '3', '3', 30, '分1', '60', '0'); insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('4', '4', '4', 40, '分1', '50', '0'); insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('5', '5', '5', 50, '分2', '40', '0'); insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE) values ('6', '6', '6', 60, '分2', '30', '0');
insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('1', '1', '1', 10, '總部', '90', '0');insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('2', '2', '2', 20, '總部', '70', '0');insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('3', '3', '3', 30, '分1', '60', '0');insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('4', '4', '4', 40, '分1', '50', '0');insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('5', '5', '5', 50, '分2', '40', '0');insert into te (ID, T_CODE, T_NAME, T_AMOUNT, T_DEPT, T_PROJECT, T_TYPE)values ('6', '6', '6', 60, '分2', '30', '0');
3.查詢全表查看
[java] view plain copy print ? select * from te;
select * from te;
4.用t.t_dept, t.t_project進行分組查詢
Sql代碼
[sql] view plain copy print ? select t.t_dept, t.t_project, sum(t.t_amount) from te t group by t.t_dept, t.t_project;
select t.t_dept, t.t_project, sum(t.t_amount) from te tgroup by t.t_dept, t.t_project;
結果如下:
5.用t.t_dept, t.t_project,並使用t.t_dept來做小計
Sql代碼
[sql] view plain copy print ? select t.t_dept, t.t_project, sum(t.t_amount) from te t group by grouping sets ((t.t_dept, t.t_project), t.t_dept);
select t.t_dept, t.t_project, sum(t.t_amount) from te tgroup by grouping sets ((t.t_dept, t.t_project), t.t_dept);
結果如下:
6.用t.t_dept, t.t_project,並使用t.t_dept來做小計,並做一次總計
Sql代碼
[sql] view plain copy print ? select t.t_dept, t.t_project, sum(t.t_amount) from te t group by grouping sets ((t.t_dept, t.t_project), t.t_dept, null);
select t.t_dept, t.t_project, sum(t.t_amount) from te tgroup by grouping sets ((t.t_dept, t.t_project), t.t_dept, null);
結果如下:
7.使用grouping(欄位)
Sql代碼
[sql] view plain copy print ? select grouping(t.t_dept),t.t_dept, t.t_project, sum(t.t_amount) from te t group by grouping sets ((t.t_dept, t.t_project), t.t_dept, null);
select grouping(t.t_dept),t.t_dept, t.t_project, sum(t.t_amount) from te tgroup by grouping sets ((t.t_dept, t.t_project), t.t_dept, null);
結果如下:
注意: 在預存程序中null不會正確執行 需要改成()
如
Sql代碼 [sql] view plain copy print ? select grouping(t.t_dept),t.t_dept, t.t_project, t.t_type, sum(t.t_amount) from te t group by grouping sets ((t.t_dept, t.t_project,t.t_type), (t.t_dept,t.t_project),t.t_dept, ());
select grouping(t.t_dept),t.t_dept, t.t_project, t.t_type, sum(t.t_amount) from te t group by grouping sets ((t.t_dept, t.t_project,t.t_type), (t.t_dept,t.t_project),t.t_dept, ());