建表:group_test ;字段:room_id 车间; job 岗位; name 姓名;salary 薪资;
create table group_test (room_id int, job varchar2(10), name varchar2(10), salary int);
插入数据:
insert into group_test values (10,'Coding', 'Bruce',1000);
insert into group_test values (10,'Programmer','Clair',1000);
insert into group_test values (10,'Architect', 'Gideon',1000);
insert into group_test values (10,'Architect', 'Gideon',1000);
insert into group_test values (20,'Coding', 'Jason',2000);
insert into group_test values (20,'Programmer','Joey',2000);
insert into group_test values (20,'Architect', 'Martin',2000);
insert into group_test values (30,'Coding', 'Rebecca',3000);
insert into group_test values (30,'Programmer','Rex',3000);
insert into group_test values (30,'Architect', 'Richard',3000);
insert into group_test values (40,'Coding', 'Samuel',4000);
insert into group_test values (40,'Programmer','Susy',4000);
insert into group_test values (40,'Architect', 'Tina',4000);
1 group by
分组函数,仅仅分组,不总计
select room_id 车间, job 岗位,sum(salary) from group_test group by room_id, job ORDER BY room_id,job ;
结果:
车间 岗位 sum(salary)
10 Architect 1000
10 Coding 1000
10 Programmer 2000
20 Architect 2000
20 Coding 2000
20 Programmer 2000
30 Architect 3000
30 Coding 3000
30 Programmer 3000
40 Architect 4000
40 Coding 4000
40 Programmer 4000
2 group by rollup
分组函数,小计(只按第一个元素小计)+合计
rollup(a,b) 统计列包含:(a,b)、(a)、()
rollup(a,b,c) 统计列包含:(a,b,c)、(a,b)、(a)、()
select room_id 车间, job 岗位,sum(salary) from group_test group by rollup (room_id, job) ORDER BY room_id,job ;
结果:
车间 岗位 sum(salary)
10 Architect 2000
10 Coding 1000
10 Programmer 1000
10 (null) 4000 -------按车间小计
20 Architect 2000
20 Coding 2000
20 Programmer 2000
20 (null) 6000 -------按车间小计
30 Architect 3000
30 Coding 3000
30 Programmer 3000
30 (null) 9000 -------按车间小计
40 Architect 4000
40 Coding 4000
40 Programmer 4000
40 (null) 12000 -------按车间小计
(null) (null) 31000 -------薪资总计
3 group by cube
分组函数,多维度小计+合计
cube(a,b) 统计列包含:(a,b)、(a)、(b)、()
cube(a,b,c) 统计列包含:(a,b,c)、(a,b)、(a,c)、(b,c)、(a)、(b)、(c)、()
select room_id 车间, job 岗位,sum(salary) from group_test group by cube (room_id, job) ORDER BY room_id,job ;
结果:
车间 岗位 sum(salary)
10 Architect 2000
10 Coding 1000
10 Programmer 1000
10 (null) 3000
20 Architect 2000
20 Coding 2000
20 Programmer 2000
20 (null) 6000
30 Architect 3000
30 Coding 3000
30 Programmer 3000
30 (null) 9000
40 Architect 4000
40 Coding 4000
40 Programmer 4000
40 (null) 12000
(null) Architect 11000
(null) Coding 10000
(null) Programmer 10000
(null) (null) 31000