当前位置:Gxlcms >
数据库问题 >
Oracle分组小计、总计示例(grouping sets的使用)
Oracle分组小计、总计示例(grouping sets的使用)
时间:2021-07-01 10:21:17
帮助过:7人阅读
- 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
- 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
- select * from te;
4.用t.t_dept, t.t_project进行分组查询
Sql代码
[sql] view plain
copy
- select t.t_dept, t.t_project, sum(t.t_amount) from te t
- group by t.t_dept, t.t_project;
结果如下:
5.用t.t_dept, t.t_project,并使用t.t_dept来做小计
Sql代码
[sql] view plain
copy
- 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);
结果如下:
6.用t.t_dept, t.t_project,并使用t.t_dept来做小计,并做一次总计
Sql代码
[sql] view plain
copy
- 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);
结果如下:
7.使用grouping(字段)
Sql代码
[sql] view plain
copy
- 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);
结果如下:
注意: 在存储过程中null不会正确执行 需要改成()
如
Sql代码
[sql] view plain
copy
- 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, ());
Oracle分组小计、总计示例(grouping sets的使用)
标签:分组 data- 数据 create nbsp clip word csdn 分享