last date for each user sql 1

last date for each user sql

select t.username, t.date, t.value
from MyTable t
inner join (
    select username, max(date) as MaxDate
    from MyTable
    group by username
) tm on t.username = tm.username and t.date = tm.MaxDate

Here is what the above code is Doing:
1. The inner query is getting the max date for each username.
2. The outer query is joining the table with the inner query to get the rows with the max date for each username.

Similar Posts