oracle group by中cube和rollup字句的使用方法及区别

时间:2022-07-15 05:39:23

  oracle group by中rollup和cube的区别: 
  Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。

  如果是ROLLUP(A, B, C)的话,先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作;

  如果是GROUP BY CUBE(A, B, C),则首先会对(A、B、C)进行GROUP BY,然后依次是(A、B),(A、C),(A),(B、C),(B),(C), 最后对全表进行GROUP BY操作。

  测试数据: 
/* 
  Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。 
*/ 
--创建表 

create table fzq
(
name varchar(20),
calss varchar(2),
kemu varchar(4),
chengji varchar(3) )

测试表

--插入数据

insert into fzq values
('','','数学','');
insert into fzq values
('','','语文','');
insert into fzq values
('','','数学','');
insert into fzq values
('','','语文','');
insert into fzq values
('','','数学','');
insert into fzq values
('','','语文','');
insert into fzq values
('','','数学','');
insert into fzq values
('','','语文',''); commit;

测试数据

  • select * from fzq;

oracle group by中cube和rollup字句的使用方法及区别

  • select calss,name,sum(chengji) from fzq group by calss,name;

oracle group by中cube和rollup字句的使用方法及区别

  • select calss,name,sum(chengji) from fzq group by cube(calss,name) order by calss;

oracle group by中cube和rollup字句的使用方法及区别

  • select calss,name,sum(chengji) from fzq group by rollup(calss,name) order by calss;

oracle group by中cube和rollup字句的使用方法及区别

  • select calss,name,sum(chengji) from fzq group by grouping sets(calss,name);

oracle group by中cube和rollup字句的使用方法及区别