成绩表有三个字段:学号、班级、成绩,如何将每个班的前三名的信息取出来?
解决方案 »
- 新年快乐
- 有关显示器亮度控制的问题,API高手也请进,大家帮帮小弟!
- 各位现在中级delphi水平一个月到底有多少呀?
- !!!!!高分在线等待高手解答EXPRESSBAR的Proxies和FLATBITTFlatSpeedButton 问题
- 散分,求delphi控件Twebbrowser做的网页浏览器源程序!!!
- 网上购物该去哪儿?
- 来来来,看看我的HOOK,怎么什么都HOOK不到啊?
- 急!!!屏蔽ctrl+alt+del还有问题请教各位高手!(nt下)
- 请问如何获得可执行文件所在的路径名
- 帮帮我 我刚学delphi !!!!!谁知道那里有他的中文帮助?!
- 有关smtp发送邮件的所有Windows API ?
- application.messagebox 弹出后,为什么要连续2次确认才能执行后面的语句?
group by 班级
order by 成绩
group by 班级
2. top是指最后结果集的前几条,不是分组的前几条
3. order by不能写在group by前面
Declare @t table(id int,StudentNum varchar(10),class varchar(10),score dec(4,2))
insert @t select 1,'001','01-1',10
union all select 2,'002','01-1',20
union all select 3,'003','01-1',30
union all select 4,'004','01-3',40
union all select 5,'005','01-3',50
union all select 6,'006','01-3',60
union all select 7,'007','01-1',70
union all select 8,'008','01-3',80
--sql statement
select *
from @t A
where id in (select top 3 id from @t where class=A.class order by id desc )
order by class,score desc
select *
from @t A
where not exists
(select 1 from @t where class=A.class and score<A.score having count(1)<1)
order by class,score desc