当前位置: 代码迷 >> 综合 >> oraclenbsp;groupnbsp;by中cube…
  详细解决方案

oraclenbsp;groupnbsp;by中cube…

热度:2   发布时间:2023-12-20 09:59:39.0

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

cube rollup grouy by
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操作。


测试数据:

--创建表
create table fzq
(
name varchar(20),
calss varchar(2),
kemu varchar(4),
chengji varchar(3)

)
--插入数据
insert into fzq values
('11','1','数学','90');
insert into fzq values
('11','1','语文','68');
insert into fzq values
('12','1','数学','90');
insert into fzq values
('12','1','语文','88');
insert into fzq values
('22','2','数学','70');
insert into fzq values
('22','2','语文','88');
insert into fzq values
('33','2','数学','95');
insert into fzq values
('33','2','语文','98');

--测试SQL
select * from fzq;
select calss,name,sum(chengji) from fzq group by calss,name;
select calss,name,sum(chengji) from fzq group by cube(calss,name) order by calss;
select calss,name,sum(chengji) from fzq group by rollup(calss,name) order by calss;
select calss,name,sum(chengji) from fzq group by grouping sets(calss,name);
  相关解决方案