日期:2014-05-18  浏览次数:20494 次

一个看似简单的问题。我不会
有一表a,有三个字段:
f1 f2 f3
1 a 01
1 b 02
1 g 03
2 c 01
2 d 02
2 e 03
3 f 01
想每个相同的f1只取第一条数据

结果:
f1 f2 f3
1 a 01
2 c 03
3 f 01


------解决方案--------------------
--按记录顺序取第一条
select * from tb a where f3=(select top 1 f3 from tb where f1=a.f1)

--取最小
select * from tb a where f3=(select min(f3) from tb where f1=a.f1)

--取最大
select * from tb a where f3=(select max(f3) from tb where f1=a.f1)

--随机取
select * from tb a where f3=(select top 1 f3 from tb where f1=a.f1 order by newid())