sql 用Group by分组后,取每组的前几条记录

--查询每门课程的前2名成绩 
CREATE TABLE StudentGrade(
stuId CHAR(4),    --学号
subId INT,        --课程号
grade INT,        --成绩
PRIMARY KEY (stuId,subId)
)
GO
--表中数据如下
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('001',1,97);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('001',2,50);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('001',3,70);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('002',1,92);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('002',2,80);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('002',3,30);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('003',1,93);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('003',2,95);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('003',3,85);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('004',1,73);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('004',2,78);
INSERT INTO StudentGrade(stuId,subId,grade) VALUES('004',3,87);
GO
/*
要查询每门课程的前2名成绩
001 1 97
003 1 93
003 2 95
002 2 80
004 3 87
003 3 85
如何实现?
*/
--查看数据
select * from StudentGrade
 
 
--假如出现并列时,也只取两个同学的话。
--方法一:
select distinct *
from studentgrade as t1
where stuid in
(select top 2 stuid
 from studentgrade as t2
 where t1.subid=t2.subid
         order by t2.grade desc)
order by subid, grade desc
 
--方法二:
select * from StudentGrade a where (select count(1) from studentGrade where subId=a.subId and grade>=a.grade)<=2
 
--方法三:
select * from StudentGrade t
where (select count(1) from StudentGrade where subid=t.subid and grade>t.grade)<=1
order by subId,grade desc
 
--结果
/*
stuId subId       grade       
----- ----------- ----------- 
001   1           97
003   1           93
003   2           95
002   2           80
004   3           87
003   3           85
 
(6 row(s) affected)
*/
 
共有三种方案,从难易程度上讲我倾向于后两种,从查询逻辑思想上来讲后两种是一样的
 
select * from StudentGrade t
where (select count(1) from StudentGrade where subid=t.subid and grade>t.grade)<=1
order by subId,grade desc
 
我是这样理解的,看成两张表A和B,条件为A表的学科=B表的学科,select count(1) from StudentGrade where subid=t.subid and grade>t.grade,返回A表的学科=B表的学科并且A表的成绩小于B表的成绩的影响行数,如果所影响的行数为零说明它的成绩是最高的,如果等于1的话就是最高的两个成绩。这就是查询条件,再按 subId,grade 排序。这种查询思想很值得我学习
  

 

转载于:https://www.cnblogs.com/SmilePastaLi/p/7281045.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值