Můžete to provést pomocí ROW_NUMBER
přes TransDate
pro každé 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
Tento dotaz získá nejnovější informace o transakci pro každé UserId
.