2015-06-20 74 views
1
  • enter image description here

考慮用戶日誌表如上。我將用戶的登錄或註銷操作保存到此表中。我如何檢索在線用戶和登錄時間?檢索在線用戶通過登錄和註銷記錄

+0

你是什麼意思在線?現在在線? –

+0

是的。我需要找到已經登錄但尚未註銷的用戶。 – Behnam

回答

1

事情是這樣的:

select * from UserLog l1 
where Operation = 'Enter' and 
     not exists(select * from UserLog l2 
       where l1.user = l2.user and 
         l2.Operation = 'Exit' and 
         l2.Time > l1.Time) 
1

我認爲這應該爲你工作:

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER (PARTITION BY UL.id ORDER BY UL.Time DESC) AS RN, UL.* 
    FROM dbo.UserLog AS UL 
    ) AS T 
WHERE T.RN = 1 
    AND T.Operation = 'Enter'; 

通過用戶名和排序最後動作只是時間分割你的表 - 如果它是Enter,那麼他就必須在線。