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)

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

posted @ 2018-08-01 16:19  张追逐  阅读(1473)  评论(0编辑  收藏  举报