select * from Glog2 a where a.Gdwork=@MGdwork and not exists(select 1 from Glog2 where Gdcode=a.Gdcode and Gdclass=a.Gdclass and Gdwork=a.Gdwork and Gdate>a.Gdate) 还能怎么写,占用资源少些???
----------------------------------------------
free
很多种写法,度娘里满地都是,例如 通用的 select * from xxx where id in (select max(id) from xxx where .... group by ...) 专用的 top 1 装B的统计开窗函数
----------------------------------------------
-
select top 1 * from Glog2 where gdclass=2 order by gdate desc select y.* from (select max(a.Gdate) Gdate,Gdwork from dbo.Glog2 a where a.Gdwork='C01' group by Gdwork) x,dbo.Glog2 y where x.Gdwork=y.Gdwork and x.Gdate=y.Gdate
with tmp as( select (row_number() over(partition by Gdcode order by gdate desc)) as num ,* from Glog2 where Gdcode in (SELECT DISTINCT Grcode FROM GrList where Grgroup='G1')) select * from tmp where num=1 问题解决
----------------------------------------------
free