Solved

SQL query for this scenario

Posted on 2013-11-28
4
170 Views
Last Modified: 2013-12-01
Hi guys,

Im sure the following can be done in a single query:

I have a table with usernames and dates when they were last active. I need a query to collect the username and the last date they were online. Each username can appear more than once and they will have various dates, ie:

Username            Date

John Smith            01/01/12
John Smith            03/11/12
John Smith            07/12/12
Simon Jones          11/06/13
Simon Jones          15/02/13
Simon Jones          18/09/13

Cheers,
Dean
0
Comment
Question by:deanlee17
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39683071
SELECT  `Username` , MAX(  `Date` )
FROM  `table_logins`
GROUP BY  `Username`
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39683073
That's assuming that the "Date" column is a date field.  If it is stored as text then you'll need to convert it to a date.  In MySQL for example you use str_to_date()
0
 

Author Comment

by:deanlee17
ID: 39683188
Yes it is in date field.

Perfect thanks.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39683202
Great - let me know if you have any more issues :)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question