时间:2021-07-01 10:21:17 帮助过:3人阅读
方法二:
1 select a.* from T1 a 2 inner join 3 (select id,min(Val) as val from T1 group by id) b 4 on a.id=b.id and a.Val=b.val
说明:方法一与方法二的作用相同,并没有区别,只不过where是隐式链接,inner join是显式链接,where逐步被边缘化。
方法三:
1 select a.* from T1 a 2 where a.val=(select min(Val) from T1 where ID=a.ID)
ID相同,取最小的Val值。
方法四:
1 select a.* from T1 as a where not exists (select * from T1 b where b.Val<a.Val and b.ID=a.ID)
拓展:还是按照字段分组,选择第一条出现的数据:
想实现的效果:
方法:
select a.* from T1 as a where val=(select top 1 Val from T1 b where a.ID=b.ID)
Sql按照字段分组,选取其他字段最值所在的行记录
标签: