我有一个包含所有日志文件的数据库供用户进入和离开办公室 目前,我使用min和max函数计算每个用户每天的工作时间。
如果我需要计算用户留在办公室的确切时间,我可以从哪里开始?
目前我不知道如何开始。
并非每个人都有一个进出的记录。有时系统可能有2 IN或2 Out。 如果发生这种情况,那么该日期的工作时间将为o。
Date Occurred Time Occurred First Name Last Name Location 9/1/2014 10:39:40 User A User A OfficeIn 9/1/2014 12:34:36 User A User A OfficeOut 9/1/2014 12:37:49 User A User A OfficeIn 9/1/2014 12:39:51 User A User A OfficeOut 9/1/2014 12:42:19 User A User A OfficeIn 9/1/2014 14:09:32 User A User A OfficeIn 9/1/2014 16:15:30 User A User A OfficeOut 9/1/2014 16:17:40 User A User A OfficeIn 9/1/2014 17:43:43 User A User A OfficeOut
答案 0 :(得分:0)
因此,对于显示顺序中的记录,您希望查看上一条记录以计算时间跨度。 LAG是查看上一条记录的SQL函数。
对于上一条记录为OfficeIn的每个OfficeOut记录,我计算时差。然后我总结每个用户获得的所有时间部分。当你分开日期和时间(为什么???)时,我必须首先将它们组合起来。由于表格中没有人员ID,只有名字和姓氏,我们希望他们的组合是唯一的。
select
name,
sum
(
case when location = 'OfficeOut'
and lag(location) over(partition by name order by when) = 'OfficeIn' then
datediff(minutes, lag(when) over(partition by name order by when)
end
) as total_minutes
from
(
select
concat(first_name, last_name) as name,
location,
cast(date_occurred as datetime) + cast(time_occurred as datetime) as when
from mytable
)
group by name
order by name;
现在进行错误检测。如果存在不一致,您希望总和为0。因此,我必须查找状态与上一条记录相同的记录。
select
name,
case when max(case when location = lag(location) over(partition by name order by when) then 'ERROR' end) = 'ERROR' then
0
else
sum
(
case when location = 'OfficeOut'
and lag(location) over(partition by name order by when) = 'OfficeIn' then
datediff(minutes, lag(when) over(partition by name order by when)
end
)
end as total_minutes
from
(
select
concat(first_name, last_name) as name,
location,
cast(date_occurred as datetime) + cast(time_occurred as datetime) as when
from mytable
)
group by name
order by name;
答案 1 :(得分:-1)
检查以下链接,它可能对您有帮助 - 您将了解如何执行此操作 -