2013-02-14 64 views
1

我需要修復一個SQL查詢,它只返回最新登錄的用戶,而不是所有登錄用戶從記錄表中返回。我正在使用SQL Server 2008 R2。從單表中返回最新登錄設備列表

表看起來是這樣的:

ID StoreID DeviceTimeStamp CreatedBy CreatedDate UpdatedBy UpdatedDate action PersonID Description 
---------------------------------------------------------------------------------------------------------------------- 
56 82 2/11/13 8:43 Handheld Login 2/11/13 8:43 Handheld Login 2/11/13 8:43 1 97 Login 
57 82 2/11/13 8:45 Handheld Login 2/11/13 8:45 Handheld Login 2/11/13 8:45 1 97 Inventory Recorded 
58 82 2/11/13 8:59 Handheld Login 2/11/13 8:59 Handheld Login 2/11/13 8:59 1 97 Logout 
59 82 2/11/13 9:12 Handheld Login 2/11/13 9:12 Handheld Login 2/11/13 9:12 1 97 Login 
60 82 2/11/13 9:14 Handheld Login 2/11/13 9:14 Handheld Login 2/11/13 9:14 1 97 Issue Reported 
61 82 2/11/13 9:14 Handheld Login 2/11/13 9:14 Handheld Login 2/11/13 9:14 1 97 Issue Reported 
62 82 2/11/13 9:22 Handheld Login 2/11/13 9:22 Handheld Login 2/11/13 9:22 1 97 Logout 
63 82 2/11/13 10:42 Handheld Login 2/11/13 10:42 Handheld Login 2/11/13 10:42 1 97 Login 
64 82 2/11/13 10:42 Handheld Login 2/11/13 10:42 Handheld Login 2/11/13 10:42 1 97 Logout 
65 82 2/11/13 11:18 Handheld Login 2/11/13 11:18 Handheld Login 2/11/13 11:18 1 97 Login 
66 82 2/11/13 11:18 Handheld Login 2/11/13 11:18 Handheld Login 2/11/13 11:18 1 97 Logout 
67 NULL 2/11/13 11:19 Handheld Login 2/11/13 11:19 Handheld Login 2/11/13 11:19 1 97 Travel Time 
68 NULL 2/11/13 11:19 Handheld Login 2/11/13 11:19 Handheld Login 2/11/13 11:19 1 97 Travel Time (Logout) 
69 NULL 2/11/13 11:19 Handheld Login 2/11/13 11:19 Handheld Login 2/11/13 11:19 1 97 Absent 
70 NULL 2/11/13 11:20 Handheld Login 2/11/13 11:20 Handheld Login 2/11/13 11:20 1 97 Absent (Logout) 
71 82 2/11/13 11:46 Handheld Login 2/11/13 11:46 Handheld Login 2/11/13 11:46 1 97 Login 
72 82 2/11/13 13:14 Handheld Login 2/11/13 13:14 Handheld Login 2/11/13 13:14 1 97 Logout 
73 NULL 2/11/13 13:14 Handheld Login 2/11/13 13:14 Handheld Login 2/11/13 13:14 1 97 Travel Time 
74 NULL 2/11/13 13:17 Handheld Login 2/11/13 13:17 Handheld Login 2/11/13 13:17 1 97 Travel Time (Logout) 
75 82 2/11/13 13:17 Handheld Login 2/11/13 13:17 Handheld Login 2/11/13 13:17 1 97 Login 
76 82 2/12/13 11:09 Handheld Login 2/12/13 11:09 Handheld Login 2/12/13 11:09 1 97 Login 
77 82 2/12/13 11:10 Handheld Login 2/12/13 11:10 Handheld Login 2/12/13 11:10 1 97 Logout 
78 82 2/13/13 15:27 Handheld Login 2/13/13 15:27 Handheld Login 2/13/13 15:27 1 97 Login 
79 52 2/13/13 15:49 Handheld Login 2/13/13 15:49 Handheld Login 2/13/13 15:49 1 97 Login 

從我試圖獲得有關誰是當前登錄到什麼位置的最新信息表。我正在使用的腳本僅返回有關最後一個登錄人員的信息,而不是其他人員。從上面,應該有1個人(PersonID)登錄到2個位置(StoreID)。

我試着刪除DISTINCT並獲取最後一個登錄人員列表中的行數。 (錯誤)。

我不擅長聚合函數,所以在修復這個查詢方面的任何幫助是非常值得歡迎的。

現有代碼 - SQL

select DISTINCT Lin.ID, 
      Lin.PersonID, 
      Lin.StoreID, 
      MAX(Lin.DeviceTimeStamp)as DeviceTimeStamp, 
      Lin.CreatedBy, 
      Lin.CreatedDate, 
      Lin.UpdatedBy, 
      Lin.UpdatedDate, 
      Lin.action, 
      Lin.description 
from 
    (select ID, 
     PersonID, 
     StoreID, 
     MAX(DeviceTimeStamp) as DeviceTimeStamp, 
     CreatedBy, 
     CreatedDate, 
     UpdatedBy, 
     UpdatedDate, 
     action, 
     Description 
    from KF_EDC_Logging 
    where 
     Description = 'Login' 
    group by PersonID, 
     StoreID, 
     DeviceTimeStamp, 
     ID, 
     CreatedBy, 
     CreatedDate, 
     UpdatedBy, 
     UpdatedDate, 
     action, 
     description) Lin 
full outer join 
    (select PersonID, 
     DeviceTimeStamp as DateTimeOut, 
     CreatedBy, 
     CreatedDate, 
     UpdatedBy, 
     UpdatedDate, 
     action, 
     description 
    from KF_EDC_Logging 
    where 
     Description = 'Logout') out 
    on Lin.PersonID = out.PersonID 
Where 
    (out.DateTimeOut is null OR Lin.DeviceTimeStamp in 
     (Select MAX(KF_EDC_Logging.DeviceTimeStamp) from KF_EDC_Logging)) 
Group BY Lin.DeviceTimeStamp, 
     Lin.PersonID, 
     Lin.StoreID, 
     out.DateTimeOut, 
     Lin.ID, 
     Lin.CreatedBy, 
     Lin.CreatedDate, 
     Lin.UpdatedBy, 
     Lin.UpdatedDate, 
     Lin.action, 
     Lin.description 

回答

0

這裏是我的嘗試。它使用公用表表達式來計算上次登錄的日期,然後將結果連接回其餘的數據。

WITH LogIn 
AS 
(
    SELECT PersonID, StoreID, MAX(DeviceTimeStamp) AS [DeviceTimeStamp] 
    FROM KF_EDC_Logging 
    WHERE Description = 'Login' 
    GROUP BY PersonID, StoreID 
), 
LogOut 
AS 
(
    SELECT PersonID, StoreID, MAX(DeviceTimeStamp) AS [DeviceTimeStamp] 
    FROM KF_EDC_Logging 
    WHERE Description = 'Logout' 
    GROUP BY PersonID, StoreID 
) 
SELECT K.ID, K.StoreID, LogIn.[DeviceTimeStamp], K.CreatedBy, 
    K.CreatedDate, K.UpdatedBy, K.UpdatedDate, K.action, 
    K.PersonID, K.Description 
FROM LogIn 
LEFT JOIN LogOut 
    ON LogIn.PersonID = LogOut.PersonID 
    AND LogIn.StoreID = LogOut.StoreID 
INNER JOIN KF_EDC_Logging AS K 
    ON LogIn.PersonID = K.PersonID 
    AND LogIn.StoreID = K.StoreID 
    AND LogIn.DeviceTimeStamp = K.DeviceTimeStamp 
WHERE (LogIn.[DeviceTimeStamp] > LogOut.[DeviceTimeStamp]) 
    OR (LogIn.[DeviceTimeStamp] IS NOT NULL AND LogOut.[DeviceTimeStamp] IS NULL) 

SQL Fiddle here

+0

神奇。感謝您及時的回覆。 – 2013-02-14 18:21:18