FieldA      FieldB
张三         57
张三         58
李四         23
李四         21 
李四         26上面为一张表,两字段,有5条记录值,
如何通过sql语句获得如下结果集(即分组中的第一条记录):张三  57
李四  23

解决方案 »

  1.   


     SELECT  FieldA ,
             FieldB
     FROM    ( SELECT    ROW_NUMBER() OVER ( PARTITION BY FieldA ORDER BY FieldA ) id ,
                         *
               FROM      biao
             ) b
     WHERE   id = 1
      

  2.   

    ;with c1 as
    (
    select row_number() over(PARTITION BY FieldA ORDER BY FieldA) rowid,
           tb.*
    from tb
    )
    select fieldA, fieldB
    from c1
    where rowid=1
      

  3.   

    --sql2000的话如果有ID且唯一select FieldA,FieldB from tb a where not exists(select FieldA,FieldB from tb where 
    a.FieldA=FieldA and a.id>id)select FieldA,FieldB from tb  where id=(select min(id) from tb group by FieldA)