Oracle数据库之group by:按自定规则分组

在数据库分组查询group by 中,默认的方式有按某一个字段的均分查询(如按班级编号分组,每一个班分组,每两个班分组。。。)

在这里写出一种按照自定义规则分组的方式:

以下语句,按自定义的格式将大于等于18个班级的学生分为4类,统计每个类别的学生数量。当然,同类未必要班级号相连,分类

条件写在when ...then之间 then之后的标志作为分类名称。

表格:

create table student
(
xh varchar2(10) primary key,
class_no number(6) not null
);

分组统计:

select 
case 
	when class_no>=1 and class_no<=2 then 1
	when class_no>=3 and class_no<=6 then 2
	when class_no>=7 and class_no<=17 then 3
	when class_no>=18 then 4
end
as class_fenlei,--班级分类
count(xh)--每个班级分类的学生人数
 from student  
group by (
case 
	when class_no>=1 and class_no<=2 then 1
	when class_no>=3 and class_no<=6 then 2
	when class_no>=7 and class_no<=17 then 3
	when class_no>=18 then 4
end
);

 

 

转载请

注明出处
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值