Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Last Logins

Author  Topic 

meemo
Starting Member

9 Posts

Posted - 2012-04-13 : 10:55:03
Hi,

I have a table which record an entry each time a user logs in the system. So, for example, a user can log in 1 or more times and it makes 1 or more entries to a table

ID | AccessDate
--------------------------------
1 | Apr 11 2012 4:18PM
2 | Apr 11 2012 4:28PM
1 | Apr 11 2012 4:38PM
2 | Apr 11 2012 4:18PM
3 | Apr 11 2012 4:38PM
1 | Apr 12 2012 4:18PM

I'm trying to figure out how to get a list of all employees, order by id, but only show the last date they logged in. So, for example, the query would return the below for the above records

ID | AccessDate
--------------------------------
1 | Apr 12 2012 4:18PM
2 | Apr 11 2012 4:28PM
3 | Apr 11 2012 4:38PM

Help?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-13 : 11:29:02
This should work if AccessDate is a datetime data type.
SELECT
ID,
MAX(AccessDate) AS LatestAccessDate
FROM
YourTable
GROUP BY
ID
ORDER BY
ID;
Go to Top of Page

meemo
Starting Member

9 Posts

Posted - 2012-04-13 : 11:46:15
Thanks, it worked!
Go to Top of Page
   

- Advertisement -