如何计算在SQL中花费的总小时数?例如,对于此员工,在此期间花费的总小时数为12-2=10。
EmpID Enter/Exit Time
2999 Entry 06:00AM
2999 Exit 12:00PM
2999 Entry 01:00PM
2999 Exit 03:00PM
2999 Entry 04:00PM
2999 Exit 06:00PM发布于 2019-04-11 08:33:11
在SQL Server2008中,这是痛苦的。假设进入/退出值已连接,并且时间实际上是有效的日期时间,则可以执行以下操作:
select empid,
sum(datediff(hour, t.time, t2.time)) as sum_hours
from t cross apply
(select top (1) t2.*
from t t2
where t2.empid = t.empid and
t2.enter_exit = 'exit' and
t2.time > t.time
) as next_time
where t.enter_exit = 'enter'
group by empid;https://stackoverflow.com/questions/55623047
复制相似问题