查找给定间隔内的记录数

时间:2015-04-20 14:18:45

标签: sql-server dateinterval

我有一个包含用户登录的表,我想找到在5天内登录该站点3次以上的用户。

例如我的表是:

id | user_id | login_date 
---+---------+--------------
 1 |    10   | 10.1.2014 00:00
 2 |    10   | 11.1.2014 10:10
 3 |    12   | 11.1.2014 11:00
 4 |    10   | 11.1.2014 12:00
 5 |    12   | 12.1.2014 00:00
 6 |    10   | 13.1.2014 10:00
 7 |    12   | 18.1.2014 00:00
 8 |    12   | 22.1.2014 09:00

对于此示例表,我想选择user_id 10,因为他/她在5天内登录了3次以上。

你可以帮助我吗?

编辑:我忘了提到数据库是sql server 2008

3 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server 2012+,则可以使用LEAD窗口函数计算任意3个连续记录之间的天数差异:

select distinct USER_ID
from (
select USER_ID, 
       datediff(d, login_date,
                   LEAD(login_date, 2) OVER (PARTITION BY user_id 
                                             ORDER BY login_date)) as diffDates
from users ) t
where t.diffDates <= 5

然后只需选择天数差异等于或小于5的USER_IDs

SQL Fiddle Demo

如果您使用的是SQL Server 2008或2005,则可以将ROW_NUMBER与自联接结合使用,以模拟LEAD函数:

;WITH CTE AS (
   SELECT id, user_id, login_date,
          ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date) AS rn
   FROM users
) 
SELECT DISTINCT user_id
FROM (             
   SELECT c1.user_id, 
          DATEDIFF(d, c1.login_date, c2.login_date) AS diffInDays
   FROM CTE AS c1
   INNER JOIN CTE AS c2 ON c1.user_id = c2.user_id AND c1.rn = c2.rn - 2
  ) t
WHERE t.diffInDays <= 5
上述查询中的

diffInDays实际上是任意3次连续用户登录之间的滚动差异。

SQL Fiddle Demo

答案 1 :(得分:1)

你可以自己加入user_id上的表,JOIN记录加入记录的5天内的记录,如下所示:

CREATE TABLE #login
    (
      id INT ,
      user_id INT ,
      login_date DATETIME
    )

INSERT  INTO #login
        ( id, user_id, login_date )
VALUES  ( 1, 10, '2014-01-10 00:00' ),
        ( 2, 10, '2014-01-11 10:10' ),
        ( 3, 12, '2014-01-11 11:00' ),
        ( 4, 10, '2014-01-11 12:00' ),
        ( 5, 12, '2014-01-12 00:00' ),
        ( 6, 10, '2014-01-13 10:00' ),
        ( 7, 12, '2014-01-18 00:00' ),
        ( 8, 12, '2014-01-22 09:00' )

SELECT  t1.user_id ,
        t1.login_date AS FirstDateInLoginPeriod ,
        COUNT(t2.user_id) AS LoginCount
FROM    #login t1
        INNER JOIN #login t2 ON t2.user_id = t1.user_id
                  AND t2.login_date 
                      BETWEEN t1.login_date AND DATEADD(DAY, 5, t1.login_date)
GROUP BY t1.user_id ,
        t1.login_date
HAVING  COUNT(t2.user_id) > 3

DROP TABLE #login

产地:

user_id  FirstDateInLoginPeriod     LoginCount
----------------------------------------------
10       2014-01-10 00:00:00.000    4

答案 2 :(得分:0)

您可以使用以下查询来获得结果。

如您所见,您可以使用DATEADD函数计算参考日期,并且必须使用过滤登录次数大于等于3

Select count(*),user_id 
from table_name 
where login_date>=DATEADD (day,-5,GETDATE())
group by user_id  
having COUNT(*) >=3