troubleshooting Question

MS SQL query to show nearest date

Avatar of k3vsmith
k3vsmith asked on
Microsoft SQL ServerSQL
6 Comments2 Solutions215 ViewsLast Modified:
I have a query that selects users and login times. I only care about the latest login, not all of there logins.
My current query is showing all of there logins.

SELECT  
    ROLES.[USERID],
    ROLES.[ROLE],
	Activity.[SIGNON_TIME_STAMP]
FROM    
[Database].[Schema1].[table1] ROLES
LEFT JOIN
    [Database].[Schema2].[table1] Activity on ROLES.[USERID] = Activity.[SIGNON_USERID]
Order by ROLES.USERID

I tried using GETDATE() but may not be using it correctly.

I added a WHERE clause
WHERE Activity.[SIGNON_TIME_STAMP] > GETDATE()
but that just gives me same thing well minus about 50 rows?

Id like to see
userA role Lastlogindate

but am seeing
userA role logindate1
userA role logindate2
userA role logindate3
userA role logindate4

logindate (SIGNON_TIME_STAMP) should be either todays date or closest to it.

I should also mention the SIGNON_TIME_STAMP is formatted as such: 2017-03-17 15:21:00.460

Please help
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros