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)

以部門爲分組,進行月份的最大值限制spa

相關文章
相關標籤/搜索