1

記録テーブルからすべてのログインユーザーではなく、最新のログインユーザーのみを返すSQLクエリを修正する必要があります。SQL Server2008R2を使用しています。

テーブルは次のようになります。

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

表から、現在誰がどの場所にログインしているかについての最新情報を取得しようとしています。私が使用しているスクリプトは、最後にログインした人に関する情報のみを返し、残りの人は返しません。上記から、2つの場所()に1人(PersonID)がログインしている必要があります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
4

1 に答える 1

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フィドルはこちら

于 2013-02-14T17:51:58.380 に答える