sql之取当前月最新数据

select DISTINCT a.[DeptName],[Num],a.DeptId,[EatingDate] from [dbo].[ISO_EmployeeEatingReport]  a
     where a.Id in(select max(c.MainId)  from [OA_EatingDetial] c
     inner join [ISO_EmployeeEatingReport] d on d.Id=c.MainId
where convert(char(7),c.DetialDate,20)>=convert(char(7),dateadd(MONTH,-1,GETDATE()),20) AND convert(char(7),c.DetialDate,20)<=convert(char(7),GETDATE(),20) 
AND  forMeal='开餐' and IsFinished=1 AND c.TypeId=1 
group by c.DeptId)

以部门为分组,进行月份的最大值限制

原文地址:https://www.cnblogs.com/LessIsMoreZ/p/9401931.html