如何取分组统计中的第一条记录?
FieldA FieldB
张三 57
张三 58
李四 23
李四 21
李四 26
上面为一张表,两字段,有5条记录值,
如何通过sql语句获得如下结果集(即分组中的第一条记录):
张三 57
李四 23
[解决办法]
SELECT FieldA , FieldB FROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY FieldA ORDER BY FieldA ) id , * FROM biao ) b WHERE id = 1
[解决办法]
;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
[解决办法]
--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)
[解决办法]
over...partition by