Du kan göra detta med en ROW_NUMBER
över TransDate
för varje 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
Den här frågan hämtar den senaste transaktionsinformationen för varje UserId
.