2013-07-22 42 views
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 empcode='13658' 

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

回答

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; 
+0

我試圖執行,但我只有1條記錄與我上面的查詢沒有年標記我有39個記錄! – Hearty

+0

做剩餘的38條記錄屬於往年? –

+0

39條記錄中,23條屬於2012年,其餘16條屬於今年! – Hearty