答案 0 :(得分:-1)
下面的代码段可以帮助您开始:
;WITH TotalDuration as
(
SELECT LogIN = Min(DateTime)
,LogOut = Max(DateTime)
,AgentID
FROM YourTable
Group BY AgentID
)
SELECT Duration = DATEDIFF(MINUTE, LogIN, LogOut)
,AgentId
FROM TotalDuration
ORDER BY AgentId
--TimeInState = datediff(minute, [DateTime], LEAD([DateTime], 1) OVER (Partition BY AgentID ORDER BY [DateTime]))
答案 1 :(得分:-1)
我想您知道如何使用min和max来获取总登录和注销时间......这里是您需要的LEAD和LAG。
with cte as(
select
agentid,
eventtype,
[DateTime],
datediff(mi,[DateTime],LEAD([DateTime]) over (partition by agentid order by [DateTime])) as [Time_in_state]
from yourTable)
select
agentid,
eventtype,
cast([DateTime] as Date) as TheDate,
sum(Time_in_state) as TotalTime,
from cte
group by
agentid,
eventtype,
cast([DateTime] as Date)