有表:
id   name  score
1    '张'    88
2    '张'    90
3    '王'    80
4    '王'    78
求:每个name中score最高的id,结果应该是:张--90;王--80
问:这个sql怎么写?急!

解决方案 »

  1.   

    select * 
    from tb a
    where not exists(select top 1 * from tb where name=a.name and score>a.score)
      

  2.   

    select * from tb a 
     where not exists(
       select * from tb where name=a.name and  score>a. score
    )
      

  3.   

    select * from tb a inner join
    (
     select name,score=max(score) from tb group by name
    ) b on a.name = b.name and a.score=b.score
      

  4.   

    刚才发错了,是这样的:
    有表:  
    id      name    score  
    1        '张'        88  
    2        '张'        90  
    3        '王'        80  
    4        '王'        78  
    求:每个name中score最高的id,结果应该是:2--90;3--80  
    问:这个sql怎么写?急!
      

  5.   

    create table t(id int identity(1,1),name char(8),score decimal(10,2))insert t select  '张',88  
    union all select '张',90  
    union all select '王',80  
    union all select '王',78 select a.* from t a inner join
    (
     select name,score=max(score) from t group by name
    ) b on a.name = b.name and a.score=b.score
      

  6.   


    create table BB
    (
       id int,
       name varchar(10),
       scour int
    )
    insert BB select 1  , '张'   ,88 
    insert BB select 2  , '张'    ,90 
    insert BB select 3  , '王'    ,80  
    insert BB select 4  , '王'    ,78  
      
    select id , 分数  from BB,
    (select name ,max(scour)分数 from BB group by name )CC  
    where BB.scour=CC.分数