f1 f2 f3
10 a 1
10 b 2
11 c 5
11 d 2
11 e 4
12 f 9
12 g 7
我想得到
f1 f2 f3
10 a 1
11 d 2
12 g 7
也就是说:相同的f1只取1条记录,这1条是相同f1所有记录中 f3最小的
10 a 1
10 b 2
11 c 5
11 d 2
11 e 4
12 f 9
12 g 7
我想得到
f1 f2 f3
10 a 1
11 d 2
12 g 7
也就是说:相同的f1只取1条记录,这1条是相同f1所有记录中 f3最小的
select * from 表名 a where not exists(select 1 from 表名 where f1=a.f1 and f3<a.f3)
where not exists(
select 1 from tb where f1=a.f1 and f3<a.f3
)
--or
select * from 表名 a where f3 in(select max(f3) from 表名 where f1=a.f1)--or
select * from 表名 a where (select count(distinct f3) from 表名 where f1=a.f1 and f3<=a.f3)=1
insert into @tb select 10,'a',1
insert into @tb select 10,'b',2
insert into @tb select 11,'c',5insert into @tb select 11,'d',2
insert into @tb select 11,'e',4
insert into @tb select 12,'f',9
insert into @tb select 12,'g',7
select * from @tb a
where not exists(
select 1 from @tb where f1=a.f1 and f3 <a.f3
)
f1 f2 f3
10 a 1
11 d 2
12 g 7
10 a 1
10 b 2
11 c 5
11 d 2
11 e 4
12 f 9
12 g 7
我想得到
f1 f2 f3
10 a 1
11 d 2
12 g 7
也就是说:相同的f1只取1条记录,这1条是相同f1所有记录中 f3最小的select t.* from tb t where f3 = (select min(f3) from tb where f1 = t.f1)
例如
13 h 6
14 i 6