SQL last user login date

Posted on 2008-11-15
Last Modified: 2012-05-05
I have a table that logs user logons called user_logon, with fields id, user and user_login_time.

id      user              user_description      user_login_time
1      user_a      user a                  10-10-2008 10:23
2      user_a      user a                  10-10-2008 11:25
3      user_a      user a                  11-10-2008 10:00
4      user_b      user b                  10-10-2008 10:23
5      user_b      user b                  10-10-2008 10:25
6      user_b      user b                  12-10-2008 10:00

I wish to select the last time that each user has logged on. How do I do this?
Question by:thehaze
    LVL 26

    Accepted Solution

    use aggragate funtion suc the one below.

    select user, max(user_login_time)
    from Table
    group by user
    LVL 9

    Expert Comment

    If you need user_description or extradata on your select use a subquery
    like...hope it help you

    SELECT *
    FROM user_logon 
    		 (select user, max(user_login_time) Last_login_time
                       from user_logon
                       group by user)  T
          WHERE user_logon.user = T.user 
    	    AND Last_login_time=user_logon .user_login_time 

    Open in new window


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now