我已根据Intime和outTime编写查询来计算员工的总工作时间和加班时间,但无法计算出另一列“IsAbsent”。例如如果某人没有任何具体日期,他的记录将不会出现在该特定日期,那么新的列IsAbsent应该包含否则存在。
查询:
with times as (
SELECT t1.EmplID
, t3.EmplName
, min(t1.RecTime) AS InTime
, max(t2.RecTime) AS [TimeOut]
, cast(min(t1.RecTime) as datetime) AS InTimeSub
, cast(max(t2.RecTime) as datetime) AS TimeOutSub
, t1.RecDate AS [DateVisited]
FROM AtdRecord t1
INNER JOIN
AtdRecord t2
ON t1.EmplID = t2.EmplID
AND t1.RecDate = t2.RecDate
AND t1.RecTime < t2.RecTime
inner join
HrEmployee t3
ON t3.EmplID = t1.EmplID
group by
t1.EmplID
, t3.EmplName
, t1.RecDate
)
SELECT EmplID
,EmplName
,InTime
,[TimeOut]
,[DateVisited]
,convert(char(5),cast([TimeOutSub] - InTimeSub as time), 108) totaltime
,convert(char(5), case when TimeOutSub - InTimeSub >= '08:01' then
cast(TimeOutSub - dateadd(hour, 8, InTimeSub) as time) else '00:00' end, 108) as overtime
FROM times
答案 0 :(得分:0)
我在阅读你的桌子时有点麻烦...但我们假设我们有两张桌子: 员工和时间......你需要做一个左连接:
select e.*, t.*
from employee e
left join time t on e.employee_id = t.employee_id
当t.employee_id为null时,您将没有时间记录......所以让我们构建它:
select e.*
IsAbsent = case
when t.employee_id is null then 'ABSENT'
else 'PRESENT'
end
from employee e
left join time t on e.employee_id = t.employee_id
下一步可能会对时间表(例如2014年的所有内容)施加约束,所以让我们在下面构建:
select e.*,
IsAbsent = case
when t.employee_id is null then 'ABSENT'
else 'PRESENT'
end
from employee e
left join time t on e.employee_id = t.employee_id and
datepart(y, t.timestamp) = 2014
请注意我已将约束放在联接中,这是至关重要的。
这有意义吗?