Requirement : Get the All users in the Login Table + and their LATEST Login Record from UserLogin Table for provided tenant
Current Query :
SELECT [USER].UserName , UserLogin.AttemptDate , UserLogin.LogoutDate
FROM [User] LEFT JOIN UserLogin
ON [User].UserId = UserLogin.UserId
WHERE [User].TenantId=3
ORDER BY UserLogin.LogoutDate desc
Issue : Repeats the User Name not Distinct
It seems that each UserId has more than one match in UserLogin, you have to prioritize records for a given UserId from the UserLogin and choose the one that you want in the result set:
Like in this example: