Sql server 2008 r2 如何获取今年最后一个月的记录,

Sql server 2008 r2 如何获取今年最后一个月的记录,,sql-server-2008-r2,Sql Server 2008 R2,我需要取今年最后一个月的记录,但我也要取过去几年的记录。请帮帮我 我有这样一个问题: select EmpCode,EventDate1 as EventDate,InTime, case when OutTime is null then 'N/A' else Outtime end as OutTime from TMS_HOURCALC WHERE DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate())) and

我需要取今年最后一个月的记录,但我也要取过去几年的记录。请帮帮我

我有这样一个问题:

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate()))
and empcode='13658'

GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;

你还需要检查一年的情况

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE 
DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(y, EventDate) = DATEPART(y, DATEADD(m, -1, getdate()))
and empcode='13658'

GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;

你还需要检查一年的情况

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE 
DATEPART(m, EventDate) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(y, EventDate) = DATEPART(y, DATEADD(m, -1, getdate()))
and empcode='13658'

GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;

我尝试执行,但我只得到了1条记录,上面的查询没有年份标记,我得到了39条记录!剩下的38条记录属于往年吗?在39条记录中,23条属于2012年,其余16条属于今年!谢谢你的帮助!我用yyyy替换了y,然后我得到了答案我尝试执行,但我只得到了1条记录,上面的查询没有年份标记,我得到了39条记录!剩下的38条记录属于往年吗?在39条记录中,23条属于2012年,其余16条属于今年!谢谢你的帮助!我用yyy替换了y,然后得到了答案