各科成绩排序,不分科目进行成绩排序

数据,创建表,查看数据表(代码)~

1、按成绩(不分科目)进行排序,并显示排名,Score 重复时保留名次空缺

select *,RANK()over(order by score desc)名次 from SC 

 
在这里插入图片描述
 

2、按成绩(不分科目)进行排序,并显示排名,Score 重复时合并名次(并列第…)

select *,DENSE_RANK()over(order by score desc)名次 from SC

 
在这里插入图片描述
 

3、按各科成绩(分科目)进行排序,并显示排名,Score 重复时保留名次空缺

--ways 1
select * ,
(select count(score)+1 from 
SC B where B.C# = A.C# and B.score > A.score) 名次 from 
SC A order by C#,名次
--ways 2
select *,RANK()over(order by score desc)名次 from SC where C# ='01' union all
select *,RANK()over(order by score desc)名次 from SC where C# ='02' union all
select *,RANK()over(order by score desc)名次 from SC where C# ='03' 

 
在这里插入图片描述 

3、按各科成绩(分科目)进行排序,并显示排名,重复时合并名次(并列第…)

--ways 1
select * ,
(select count(distinct score)+1 from 
SC B where B.C# = A.C# and B.score > A.score) 名次 from 
SC A order by C#,名次
--ways 2
select *,DENSE_RANK()over(order by score desc)名次 from 
SC where C# ='01' union all
select *,DENSE_RANK()over(order by score desc)名次 from 
SC where C# ='02' union all
select *,DENSE_RANK()over(order by score desc)名次 from 
SC where C# ='03' 

 
在这里插入图片描述
 

4、按各科成绩(分科目)进行排序,并显示排名,Score重复时按学号升序排名,名次不重复

select * from
(select *,ROW_NUMBER()over(partition by C# order by score desc,S#)A 
from SC)B

 
在这里插入图片描述
 

5、查询每门课程成绩最好的前两名

--ways 1
select * from SC B where (select count(score) from SC A where A.C# = B.C# and A.score > B.score  ) <2
order by C#,score desc

--ways 2
select * from
(select *,ROW_NUMBER()over(partition by C# order by score desc,S#)名次 from SC)B
where B.名次<3

 
在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值