SQL query to include last date a person used an application

Posted on 2009-04-24
Last Modified: 2012-05-06
Below is a query that displays a count on how many times individual people logged into an application within a specified time period.   For instance, John Doe logged in 20 times since March 20.  What I need to include though is the last date John Doe logged on.  I was able to get a list of all 20 times but I'm really interested in just the last date he logged on.

SELECT tblUser.strUserID AS [USER ID], tblUser.strName AS NAME, COUNT(tblAudit.dtmTDStamp) AS [LOGIN COUNT] AS [LAST LOGIN DATE]
tblAudit ON tblUser.strUserID = tblAudit.strUserID
WHERE (tblAudit.dtmTDStamp > CONVERT(DATETIME, '2009-03-20 00:00:00', 102))
GROUP BY tblUser.strUserID, tblUser.strName, tblAudit.bytRecType
HAVING (tblAudit.bytRecType = '1')

Any suggestions will be appreciated.
Question by:vlay
    LVL 92

    Accepted Solution

    Just add MAX(NameOfLoginDatetime) to your select clause (changing to the right column name first!).

    Author Closing Comment

    THANK YOU!  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    758 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

    13 Experts available now in Live!

    Get 1:1 Help Now