22,181
社区成员




select A=min(A),B
from tb
group by B
if object_id('Test') is not null drop table Test
create table Test(A int,B varchar(20))
insert Test
select 1,'a' union all
select 2,'a' union all
select 3,'b' union all
select 4,'c' union all
select 5,'c' union all
select 6,'c'
select a.*
from Test a
where a.A=(select min(b.A) from Test b Group by b.B having a.B=b.B)
select min(a) a ,b from tb group by b
select min(a),b from A group by b
declare @t table(A int, B varchar(10))
insert @t select 1, 'a'
insert @t select 2, 'a'
insert @t select 3, 'b'
insert @t select 4, 'c'
insert @t select 5, 'c'
insert @t select 6, 'c'
select * from @t t where not exists(select 1 from @t where b=t.b and a<t.a)
/*A B
----------- ----------
1 a
3 b
4 c
(3 行受影响)*/
select max(a)a ,b from tb group by b
select min(a.A) as A,b.B From a,b
where a.id=b.id
group by b.B