declare @T table (id1 int,id2 int,a int,b int,c int)
insert into @T
select 1,1,5,4,3 union all
select 2,1,8,6,4 union all
select 1,2,6,7,2 union all
select 2,2,10,5,4 union all
select 1,3,10,1,1 union all
select 2,3,12,2,2
/*
-->取最小
select * from 表 a where 某列=(select min(某列) from 表 where 分组列=a.分组列)
-->取最大
select * from 表 a where 某列=(select max(某列) from 表 where 分组列=a.分组列)
-->取第一条
select * from 表 a where 某列=(select top 1 某列 from 表 where 分组列=a.分组列)
-->随机取一条
select * from 表 a where 某列=(select top 1 某列 from 表 where 分组列=a.分组列 order by newid())
*/
select * from @T a where a=(select max(a) from @T where id2=a.id2) order by id2
sql数据分组重复的取一条
最新推荐文章于 2023-07-02 16:10:01 发布