0

I am trying to track user data for when users log in to an application. What I am trying to pull from my query (below) is all users who have not logged in for the past 30 days or more. However, it is pulling in users who have logged in quite recently. Help?

SELECT UserNM AS [UserID], MAX(EventDT) AS [Last Log-in Date]
FROM dbo.USREventLog
WHERE ABS(DATEDIFF([day], EventDT, GETDATE())) > 30

    AND (UserNM NOT LIKE 'user1') AND (UserNM NOT LIKE 'user2')
    AND (UserNM NOT LIKE 'user3') AND (UserNM NOT LIKE 'user4')

GROUP BY UserNM

BTW, I am using their most recent record of activity as their last log in date and I have a small list of users who should absolutely not be included in the results.

2 Answers2

1

Try this:

SELECT UserNM AS [UserID], MAX(EventDT) AS [Last Log-in Date]
FROM dbo.USREventLog
WHERE EventDT < GETDATE() - 30

    AND (UserNM NOT LIKE 'user1') AND (UserNM NOT LIKE 'user2')
    AND (UserNM NOT LIKE 'user3') AND (UserNM NOT LIKE 'user4')

GROUP BY UserNM, EventDT
Alaa Awad
  • 3,612
  • 6
  • 25
  • 35
  • Msg 147, Level 15, State 1, Line 3 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. – Nicholas Anderson Feb 05 '13 at 18:37
0

I've found another solution elsewhere. The query is pasted below.

SELECT UserID, [Last Login Date] from (
    SELECT UserNM AS [UserID], MAX(EventDT) AS [Last Login Date]
    FROM dbo.TSEventLog 
    WHERE (UserNM NOT LIKE 'user 1') AND (UserNM NOT LIKE 'user2')
    GROUP BY UserNM) x
WHERE ABS(DATEDIFF(day, [Last Login Date], GETDATE())) > 30
ORDER BY [UserID] ASC