34,571
社区成员
发帖
与我相关
我的任务
分享
declare @a table (a int,b int)
insert @a select
1 , 1 union all select
2 ,1 union all select
3 ,2 union all select
4 , 2 union all select
5, 2
select min(a) as a,b
from @a
group by b
declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3 union all
select 2,2 ,3,2
select * from @table
where a in (select min(a) from @table group by b)
/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
2 1 6 5
2 2 3 2
*/
declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3
--如果字段多,这样不行
select min(a) as a,b ,c,d
from @table
group by b,c,d
/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
2 1 6 5
3 2 4 3
4 2 5 4
*/
--这样可以:
select * from @table t
where a=(select min(a) from @table where b=t.b) order by b
/*
a b c d
----------- ----------- ----------- -----------
1 1 4 5
3 2 4 3
*/
declare @table table (a int,b int,c int ,d int)
insert into @table
select 1,1 ,4,5 union all
select 2,1 ,6,5 union all
select 3,2 ,4,3 union all
select 4,2 ,5,4 union all
select 5,2 ,4,3
--如果字段多,这样不行
select min(a) as a,b ,c,d
from @table
group by b,c,d
--这样可以:
select * from @table t
where a=(select min(a) from @table where b=t.b) order by b
Create table #a(a int,b int ,c int)
insert into #a
select 1,1,1 union all
select 2,1,2 union all
select 3,2,3 union all
select 4,2,4 union all
select 5,2,5
select *
from #a a
where not exists(select a from #a where b=a.b and a.a>a)
-----------结果-----------
1 1 1
3 2 3
select * from ta
where a in (select min(a) from ta group by b)
select *
from tb a
where a = (select a from tb where b=a.b and a.ID<tb.ID)
Select max(a) as a,b
from ta
group by b
或者
select min(a) as a,b
from ta
group by b
select min(a) as a,b
from ta
group by b
select *
from @T A
where not exists(select 1 from @T where b=A.b and a<A.a)
select *
from tb a
where a = (select top 1 a from tb where b=a.b)