您的位置:程序门 -> delphi -> 数据库相关



求一sql语句:取每个班前三名


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求一sql语句:取每个班前三名[已结贴,结贴人:sc_arhat]
发表于:2007-06-30 17:19:16 楼主
成绩表有三个字段:学号、班级、成绩,如何将每个班的前三名的信息取出来?
发表于:2007-06-30 19:09:181楼 得分:5
select   top   3   *   from   成绩表
group   by   班级
发表于:2007-06-30 19:10:072楼 得分:5
select   top   3   *   from   成绩表
order   by   成绩
group   by   班级
发表于:2007-07-01 00:35:373楼 得分:10
楼上的sql语句想当然地错了   -_-

1.   select   *,   group   by里面怎么只能写一个字段
2.   top是指最后结果集的前几条,不是分组的前几条
3.   order   by不能写在group   by前面
发表于:2007-07-01 00:58:084楼 得分:5
那樓上有什麽高見呢?
发表于:2007-07-01 09:44:285楼 得分:10
简单!
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
发表于:2007-07-01 09:46:556楼 得分:10
--方法2
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
发表于:2007-07-01 13:06:057楼 得分:5
有这么简单么?!     有10个人并列排名(分数一样)呢


快速检索

最新资讯
热门点击