Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

I have a table that is a collection entry as to when a user was logged on.

username, date,      value

--------------------------

brad,     1/2/2010,  1.1

fred,     1/3/2010,  1.0

bob,      8/4/2009,  1.5

brad,     2/2/2010,  1.2

fred,     12/2/2009, 1.3 etc..

How do I create a query that would give me the latest date for each user?

Update: I forgot that I needed to have a value that goes along with the latest date.

1 Answer

0 votes
by (40.4k points)

Try using the below code:

Query

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

You can master these queries and become proficient in SQL queries by enrolling in an industry-recognized SQL certification

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...