sqlserver 班级排名_Sqlserver:班级排名问题(转发)

本文介绍了如何在SQLServer中查询每个班级的前两名学生,提供了五种不同的解题方法,包括子查询、窗口函数以及交叉应用等。解法涉及到了rank()、dense_rank()、ntile()和row_number()等排名函数的使用,适用于解决数据排名和分组查询的问题。
摘要由CSDN通过智能技术生成

id,name(人名),class(班级),fen(分数)

1  jj          1          88

2  j1          1            90

3  j2            2        70

选出每个班级头两名分数, 显示班级,人名,分数????????

create table #t (id int,name varchar(20),class varchar(10),fen int)

insert into #t

select 1,'jj','1',88 union all

select 2,'j1','1',90 union all

select 3,'j2','1',70 union all

select 4,'j3','2',89 union all

select 5,'ja','3',96 union all

select 6,'jb','3',63

解法一:

select *

from #t t

where (select count(*) from #t where class="t".class and fen > t.fen) =0 -- 第一名

where (select count(*) from #t where class="t".class and fen > t.fen) =1 -- 第二名

where (select count(*) from #t where class="t".class and fen > t.fen) <2 -- 前2名

where (select count(*) from #t where class="t".class and fen < t.fen) =0 -- 倒数第一名

where (select count(*) from #t where class="t".class and fen < t.fen) =1 -- 倒数第二名

where (select count(*) from #t where class="t".class and fen < t.fen) <2 -- 倒数前2名

解法二:

select t.* from #t t where fen in (select top 2 fen from #t where class = t.class) order by class,fen

解法三:

select *

from

(

select row_number() over(partition by  class order by fen desc) as rank,* from #t

) t

where rank <=2

order by class,rank,fen

附注:排名函数

rank():稀疏排名,排名可能会并列,因此引起排名的数字间断。如有两个并列第一,则接下来的排名为第三!

dense_rank():非稀疏排名,排名可能会并列,但排名的数字不会间断。如有两个并列第一,则接下来的排名为第二!

ntile(group count):(抽屉分配)将数据按分组数分组 分配。主要是看总行数 是否 能被 组数 平均分配,如果不能平均分配,则会将数据有限分配到前面的组中!

row_number():分配唯一的逻辑行记录的id,通常用于分页用

over子句还支持聚合函数 和 窗口分区子句

解法四:

select distinct t.*

from #t a

cross apply (select top 2 * from #t b where a.class = b.class order by fen desc) as t --内部(左值)多值交叉表

order by t.class,t.fen desc

解法五:

select distinct t.*

from #t a

outer apply (select top 2 * from #t b where a.class = b.class order by fen desc) as t --外部(右值)多值交叉表

order by t.class,t.fen desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值