对至少30天的所有记录的SQL查询

时间:2013-02-05 18:28:52

标签: sql-server-2008 datediff teamsite interwoven

我正在尝试跟踪用户登录应用程序时的用户数据。我想从我的查询(下面)中提取的是所有未在过去30天或更长时间内登录的用户。但是,它吸引了最近登录的用户。帮助

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

顺便说一句,我使用他们最近的活动记录作为他们的最后登录日期,我有一小部分用户,他们绝对不应该包括在结果中。

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

我在其他地方找到了另一种解决方案。查询粘贴在下面。

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