gpt4 book ai didi

tsql - 如何编写查询来获取上周的记录

转载 作者:行者123 更新时间:2023-12-02 00:06:50 26 4
gpt4 key购买 nike

我需要从 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;

谁能帮我取上周星期天到上星期六的记录。

最佳答案

对列进行计算并在 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;

关于tsql - 如何编写查询来获取上周的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17804787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com