我需要获取今年上个月的记录,但我也得到了过去几年的记录。请帮帮我
我有这样的查询:
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;
答案 0 :(得分:1)
您还需要检查年份条件。
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;