oracle groupby and rollup cube and grouping sets
GROUP BY ROLLUP (A, B, C), it will first (A, B, C) for GROUP BY, then (A, B) for GROUP BY, then (A) for GROUP BY, and finally the whole table perform GROUP BY operation.
GROUP BY CUBE (A, B, C), it will first (A, B, C) for GROUPBY, then followed by (A, B), (A, C), (A), (B, C), (B), (C), and finally the whole table GROUP BY operation.
GROUP BY GROUPING SETS ((A, B), C), only A, B and C packets, i.e. packets designated
Three or more are used in a group by clause
grouping(param)、grouping_id(param1,param2,param3...)、group_id()
grouping (param), groupby clause columns involved in the packet, a value of 0, No. 1. param involved grouping columns.
grouping_id (param1, param2, param3 ...) grouping (param) Returns the value is converted to decimal form.
group_id () no parameters, then the set of packets identified duplicate rows, the value 012 is sequentially incremented.
create table TEST_GROUPBY
(
t_id VARCHAR2(32) not null,
type1 VARCHAR2(30),
type2 VARCHAR2(30),
type3 VARCHAR2(30),
type4 VARCHAR2(30),
type5 VARCHAR2(30),
user_salary NUMBER(20,2)
)
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('1', 'type11', 'type21', 'type31', 'type45', null, 10.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('2', 'type11', 'type22', 'type31', 'type45', null, 20.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('3', 'type11', 'type22', 'type31', 'type45', null, 30.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('4', 'type12', 'type22', 'type31', 'type45', null, 40.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('5', 'type12', 'type21', 'type31', 'type45', null, 50.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('6', 'type12', 'type21', 'type32', 'type45', null, 60.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('7', 'type12', 'type22', 'type32', 'type45', null, 70.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('8', 'type13', 'type21', 'type32', 'type45', null, 80.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('9', 'type13', 'type23', 'type32', 'type46', null, 90.00);
insert into test_groupby (T_ID, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, USER_SALARY)
values ('10', 'type14', 'type21', 'type32', 'type46', null, 100.00);
select a.* from test_groupby a;
select grouping(a.type1),grouping(a.type2),grouping_id(a.type1),grouping_id(a.type2),grouping_id(a.type1,a.type2),group_id(),
a.type1,a.type2,sum(a.user_salary)
from test_groupby a group by a.type1,cube(a.type1, a.type2) order by a.type1,a.type2;
select a.type1,a.type2,sum(a.user_salary) from test_groupby a group by a.type1, a.type2;
select a.type1,a.type2,sum(a.user_salary) from test_groupby a group by rollup(a.type1, a.type2);
select a.type1,a.type2,sum(a.user_salary) from test_groupby a group by cube(a.type1, a.type2) order by a.type1,a.type2;