select * from 表 a where f2=(select max(f2) from 表 where a.f1=f1)

解决方案 »

  1.   

    --为保证顺序,最好加上排序
    select * from 表 a where f2=(select max(f2) from 表 where a.f1=f1)
    order by f1
      

  2.   

    --数据测试--数据测试环境
    declare @t table(f1 varchar(1),f2 int,f3 int)
    insert into @t
    select 'A',7,230
    union all select 'A',8,460
    union all select 'A',9,580
    union all select 'B',7,258
    union all select 'B',8,468
    union all select 'C',7,563
    union all select 'C',9,587--查询
    select * from @t a where f2=(select max(f2) from @t where a.f1=f1)
    order by f1
    /*--结果:
    f1   f2          f3          
    ---- ----------- ----------- 
    A    9           580
    B    8           468
    C    9           587(所影响的行数为 3 行)
    --*/