有个表 table 
有三个字段  课程, 学号,成绩 
要求查询每门功课成绩最好的那条记录,要求把三的字段的内容都显示出来 
求SQL语句~~~!!!

解决方案 »

  1.   

    select 课程, 学号,max(成绩) 成绩
    from table1
    group by  课程, 学号;
      

  2.   


    ----建测试数据表
    create table LF([课程] nvarchar(10),[学号] varchar(10),[成绩] float) 
    insert into LF
    select '语文','00001',97.5
    union all
    select '语文','00002',96.5
    union all
    select '语文','00003',94
    union all
    select '数学','00001',91.5
    union all
    select '数学','00002',98.5
    union all
    select '数字','00003',93
    union all
    select '化学','00001',90.5
    union all
    select '化学','00002',86.5
    union all
    select '化学','00003',93/*
    课程      学号     成绩
    语文 00001 97.5
    语文 00002 96.5
    语文 00003 94.0
    数学 00001 91.5
    数学 00002 98.5
    数字 00003 93.0
    化学 00001 90.5
    化学 00002 86.5
    化学 00003 93.0*/取各科最高排名:select a.* from (select [课程],max([成绩]) as [成绩]from lf where [课程]='语文' GROUP BY [课程]
    union all
    select [课程],max([成绩]) as [成绩] from lf where [课程]='数学' GROUP BY [课程]
    union all
    select [课程],max([成绩]) as [成绩] from lf where [课程]='化学' GROUP BY [课程]) m,lf a
    where a.[课程]=m.[课程] and a.[成绩]=m.[成绩]---结果
    /*
    语文 00001 97.5
    数学 00002 98.5
    化学 00003 93.0*/
      

  3.   


    select 课程,MAX( 学号) AS  学号,max(成绩) AS  成绩 
    from table 
    group by  课程
      

  4.   

    借用 [GDTOPONE]-IT民工 的表结构
    select lf.课程,lf.学号,lf.成绩
    from lf,
    (
    select 课程,max(成绩) 成绩
    from lf
    group by 课程) b
    where lf.课程=b.课程
    and lf.成绩=b.成绩语文 00001 97.5
    数字 00003 93.0
    数学 00002 98.5
    化学 00003 93.0向[IT民工]学习每次都实践一下,呵呵
      

  5.   

    我不知道具体有那些课程呢,要求不能用 select'语文'  select'数学'  select'化学'  
      

  6.   

    呵呵,到了这么多常玩数据库的,试试我这个句子。select 课程,学号,成绩
    from table a
    where exists(select 课程,max(成绩) from table where 课程=a.课程 
    group by 课程 having max(成绩)=a.成绩)
      

  7.   

    select A.* From table A ,(select 课程,max(成绩) 成绩 from table group by  课程 ) bwhere A.课程=B.课程 and A.成绩=B.成绩