以下是mytable
的样子:
+-----------------+-----------+-------------+------------------+
| Time | EventType | MachineName | UserDisplayName |
+-----------------+-----------+-------------+------------------+
| 7/22/2014 6:52 | CONNECTED | MACH-10 | alice.brown |
| 7/22/2014 6:52 | PENDING | MACH-10 | alice.brown |
| 7/22/2014 9:09 | PENDING | MACH-2 | mike.hensworth |
| 7/22/2014 6:58 | CONNECTED | MACH-2 | mike.hensworth |
| 7/22/2014 6:57 | PENDING | MACH-2 | mike.hensworth |
| 7/22/2014 7:00 | CONNECTED | MACH-3 | will.garden |
| 7/22/2014 6:59 | PENDING | MACH-3 | will.garden |
| 7/22/2014 5:30 | PENDING | MACH-3 | will.garden |
| 7/22/2014 11:20 | CONNECTED | MACH-4 | kent.clark.admin |
| 7/22/2014 11:20 | PENDING | MACH-4 | kent.clark.admin |
| 7/22/2014 9:28 | CONNECTED | MACH-5 | inigo.montoya |
| 7/22/2014 9:28 | PENDING | MACH-5 | inigo.montoya |
| 7/22/2014 9:15 | PENDING | MACH-123 | inigo.montoya |
| 7/22/2014 12:37 | CONNECTED | P-002 | joseph.smith |
| 7/22/2014 12:36 | PENDING | P-002 | joseph.smith |
| 7/22/2014 9:43 | CONNECTED | P-005 | penny.pennyworth |
| 7/22/2014 9:41 | PENDING | P-005 | penny.pennyworth |
| 7/22/2014 5:41 | CONNECTED | P-006 | thor.zeus |
| 7/22/2014 5:40 | PENDING | P-006 | thor.zeus |
+-----------------+-----------+-------------+------------------+
情况是人(UserDisplayName)正在登录到计算机(MachineNames)。 Eventtype PENDING是登录的开始,EventType CONNECTED是成功连接。
基本上,我需要用户登录计算机所需的平均时间。我没有很好的方法将PENDING
状态与CONNECTED
状态相关联,因此我尝试计算CONNECTED
状态与PENDING
状态之间的时差给定MachineName
的最新inigo.montoya
EventType。
我不知道该怎么做,而这就是这个问题的关键。
但是我也遇到了我想克服和认识的问题。
(例如,PENDING
)有时用户在一台计算机上启动PENDING
,然后在需要很长时间时放弃,并在一秒钟内开始第二次CONNECTED
机。列出的PENDING
没有关闭第一个MACH-3
事件。我想确定这些事件。
(例如,PENDING
)有时CONNECTED
永远不会成为机器上的PENDING
,并且用户可以某种方式启动第二个{{1}}如预期。我想确定这些事件。
答案 0 :(得分:1)
我认为这应该做你想要的:
with successes as
(select x.userdisplayname,
x.machinename,
x.time as connection_time,
y.time as pending_time,
datediff(mi, y.time, x.time) as mins_between
from mytable x
join mytable y
on x.userdisplayname = y.userdisplayname
and x.machinename = y.machinename
where x.eventtype = 'CONNECTED'
and y.eventtype = 'PENDING'
and y.time = (select max(z.time)
from mytable z
where z.userdisplayname = y.userdisplayname
and z.machinename = y.machinename
and z.time < y.time))
select y.userdisplayname, y.machinename, null, y.time, null
from mytable y
left join successes s
on y.userdisplayname = s.userdisplayname
and s.machinename = y.machinename
and y.time = s.pending_time
where s.connection_time is null
and y.eventtype = 'PENDING'
union all
select * from successes
请注意,具有空值的行表示连接尝试失败。