我尝试计算working hours
员工,同时排除他们Break-in
的{{1}}和Break-out
。现在我使用this function来完成我的部分目标。
修改
示例问题:
员工1
时间开始:Time Reference
-
时间结束:Dec 18 2017 8:00AM
闯入:Dec 18 2017 17:00PM
- 突破:12:00 PM
请注意,“进入和退出时间”来自数据库
时,用户输入时间开始和结束12:30 PM
当前输出: 9小时
预期输出 8.5小时,因为它从总休息时间中减去
答案 0 :(得分:1)
declare @break_in time(0) = '12:00',
@break_out time(0) = '12:30'
declare @sample table
(
time_start time(0),
time_end time(0)
)
insert into @sample select '08:00', '17:00'
insert into @sample select '08:00', '11:00'
insert into @sample select '08:00', '12:15'
insert into @sample select '12:15', '17:00'
insert into @sample select '13:00', '17:00'
insert into @sample select '12:10', '12:20'
select
s.time_start, s.time_end,
total_mins = datediff(minute, time_start, time_end) / 60.0,
b.break_start, b.break_end,
total_break = isnull(datediff(minute, break_start, break_end) / 60.0, 0),
work_hours = (datediff(minute, time_start, time_end) - isnull(datediff(minute, break_start, break_end), 0))/ 60.0
from @sample s
cross apply
(
select break_start = case when @break_in between time_start and time_end
then @break_in
when time_start between @break_in and @break_out
then time_start
else NULL
end,
break_end = case when @break_out between time_start and time_end
then @break_out
when time_end between @break_in and @break_out
then time_end
end
) b