postgresql left join distinct on 1

postgresql left join distinct on

Select Distinct On (u.username, u.email)
     u.username
    ,u.email
    ,l.browser
    ,l.login_time
From users u
Join logins l On l.username = u.username
Order By u.username, u.email, login_time Desc
 
                                        ^

Here is what the above code is Doing:
1. Selecting distinct rows based on the username and email columns.
2. Ordering the rows by the username and email columns.
3. Ordering the rows by the login_time column.
4. Selecting the first row from the ordered set.

Similar Posts