これはROW_NUMBER
で行うことができます TransDate
を介して UserId
ごとに :
;With Cte As
(
Select L.[ID],
L.[UserID],
L.[Time],
L.[Action],
R.[Role],
Row_Number() Over (Partition By [L].[UserId] Order By [R].[TransDate] Desc) Row_Number
From [TEST111].[dbo].[tblLog] as L
Join [TEST111].[dbo].[tblRole] as R On L.[UserID] = R.[UserID]
)
Select [Id], [UserId], [Time], [Action], [Role]
From Cte
Where [Row_Number] = 1
このクエリは、各UserId
の最新のトランザクション情報を取得します 。