3

我需要从 sqlserver2008r2 获取记录,我获取当前一周记录的查询是:

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC WHERE intime1
  BETWEEN dateadd(dd,(datediff(DD,-53684,getdate())/7)*7,-53684) 
    AND dateadd(dd,((datediff(dd,-53684,getdate())/7)*7)+7,-53684) 
    and empcode = @empcode
GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;

任何人都请帮助我仅从上周日到上周六获取上周的记录。

4

2 回答 2

2

对列进行计算并在 where 子句中引用它会产生糟糕的性能。这是一个更好的方法:

select EmpCode,EventDate1 as EventDate,InTime,
case when OutTime is null then 'N/A' else Outtime end as OutTime from    
TMS_HOURCALC 
WHERE intime1 >= dateadd(dd,(datediff(DD,-1,getdate())/7)*7-7,-1) 
and intime1 < dateadd(dd,((datediff(dd,-1,getdate())/7)*7),-1) 
and empcode = @empcode
GROUP BY EmpCode, InTime,OutTime, EventDate1,intime1     
order by intime1;
于 2013-07-23T10:34:33.463 回答
1

确保在您的数据库中@@DateFirst设置为 7(周日默认)并使用DATEPART()

尝试使用:

where 
DATEPART(ww, intime1) = DATEPART(ww, GetDate())
and 
YEAR(intime1) = YEAR(GetDate())
于 2013-07-23T08:32:43.743 回答