通过case when自定义的段位进行统计排序

本文介绍了一种利用 SQL 中的 CASE WHEN 语句实现自定义排序的方法,并提供了一个具体示例:根据学生的分数划分不同的段位并按段位进行排序。这种方法能够灵活地对数据进行分类及汇总。

通过case when 进行自定义排序

select * from (select a.c#,cname,case when score>=85 then '100-85'
                         when score<85 and score>=70 then '85-70'
                         when score<70 and score>=60 then '70-60'
                         else '0-60'
                         end 段位 
                         ,count(1) 数量
             ,count(1)*100.0/(select count(1)from sc where c#=a.c#) 均分
from sc a 
join course b on a.c#=b.c#
group by a.c#,cname,case when score>=85 then '100-85'
                         when score<85 and score>=70 then '85-70'
                         when score<70 and score>=60 then '70-60'
                         else '0-60'
                         end) a
order by  a.c#, case 段位 
                       when  '0-60' then 1
                       when  '70-60' then 2    
                       when  '85-70' then 3    
                       when  '100-85' then 4            
                  end

结果

转载于:https://www.cnblogs.com/syhsl/p/3936669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值