所以我正在查询一些数据并试图确定每个用户登录和注销之间的时间量。我接近得到这个,但得到的结果不起作用。这是我现在拥有的代码:
WITH OrderedTable AS
(
SELECT EventType
, ModuleAndEventText
, Time
, Node
, UserSID
, ROW_Number() OVER (Partition BY UserSID ORDER BY UserSID,EventID,Time) RN
FROM viewevent
where EventType in ('BROKER_USERLOGGEDIN','BROKER_USERLOGGEDOUT') and usersid = 'S-1-5-21-999033763-294680432-740312968-10026'
)
SELECT t1.EventType
, t1.ModuleAndEventText
, t1.Node
, t1.UserSID
, t1.Time as TimeIn
, t2.Time as TimeOut
, DATEDIFF(hour, t1.Time, t2.Time ) TimeElapsedInHours
FROM OrderedTable t1
JOIN OrderedTable t2 ON t1.UserSID = t2.UserSID AND t2.RN = t1.RN + 1
WHERE t1.RN % 2 <> 0
ORDER BY t1.UserSID
以下是我得到的一些结果:
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-04-14 08:00:36.137 2012-04-14 09:32:08.267 1
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-04-14 08:00:36.137 2012-04-14 09:32:08.267 1
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-06-11 14:50:40.420 2012-06-11 16:43:08.640 2
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-06-11 17:49:46.330 2012-06-11 18:42:50.047 1
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-06-11 18:59:40.550 2012-06-12 23:20:16.027 29
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-06-12 23:20:16.777 2012-06-12 23:20:16.823 0
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026 2012-06-12 23:20:17.120 2012-06-15 13:03:31.807 62
前两条记录是正确的。最后几个不是,它选择注销的记录作为时间,我不清楚如何解决这个问题。这是数据:
EventType ModuleAndEventText Time Node UserSID
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-04-14 08:00:36.137 VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-04-14 09:32:08.267 VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-04-14 08:00:36.137 VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-04-14 09:32:08.267 VMVIEWTC1.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-06-11 14:50:40.420 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-06-11 16:43:08.640 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-06-11 17:49:46.330 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-06-11 18:42:50.047 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-06-11 18:59:40.550 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-06-12 23:20:16.027 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-06-12 23:20:16.777 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-06-12 23:20:16.823 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDOUT User YRMC_MAIN\jerogers has logged out 2012-06-12 23:20:17.120 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
BROKER_USERLOGGEDIN User YRMC_MAIN\jerogers has logged in 2012-06-15 13:03:31.807 VMVIEWWC2.yrmc.org S-1-5-21-999033763-294680432-740312968-10026
提前致谢。卡梅隆