过滤现在正在工作的同事

时间:2014-11-13 08:45:20

标签: sql sql-server date select

我有一个名为Work的表,列是

EmployeeID(key), 
Date (datetime, when the employee checked in to work),
Work hours (int, the hours he/she will be working for)

这是一个学校项目,所以我根本无法修改表格。 我必须写一个选择,回馈那些在那一刻工作的人,这是我到目前为止所得到的:

select EmployeeID as Availables, w.Date as Started at
from Work w
where datepart(year,w.Date) = DATEPART(year,getdate()) and 
datepart(month,w.Datum) = datepart(month,getdate()) and
datepart(day,w.Date) = datepart(day,getdate()) and
datepart(hour,w.Date) <= datepart(hour,getdate()) and
datepart(hour,w.Date)+w.WorkHours > datepart(hour,getdate())

这个解决方案是不对的,因为如果我在上午01:00运行查询,并且员工在前一天下午23:00开始工作(这是可能的),那么他赢了#39 ;列在清单上。 我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

您应该改变寻找当前工作员工的逻辑

select EmployeeID as Availables, w.Date as Started at
from Work w where getdate() between w.Date and DATEADD(HOUR, w.WorkHours,  w.Date)

答案 1 :(得分:0)

试试这个..

select EmployeeID as Availables, w.Date as Started at
from Work w where w.Date>= DATEADD(HH,  w.WorkHours,w.Date,) and  w.Date<=getdate()