SQL to determine minimum sequential days of access?

前端 未结 19 1705
我在风中等你
我在风中等你 2020-12-04 04:58

The following User History table contains one record for every day a given user has accessed a website (in a 24 hour UTC period). It has many thousands of r

19条回答
  •  一整个雨季
    2020-12-04 05:22

    declare @startdate as datetime, @days as int
    set @startdate = cast('11 Jan 2009' as datetime) -- The startdate
    set @days = 5 -- The number of consecutive days
    
    SELECT userid
          ,count(1) as [Number of Consecutive Days]
    FROM UserHistory
    WHERE creationdate >= @startdate
    AND creationdate < dateadd(dd, @days, cast(convert(char(11), @startdate, 113)  as datetime))
    GROUP BY userid
    HAVING count(1) >= @days
    

    The statement cast(convert(char(11), @startdate, 113) as datetime) removes the time part of the date so we start at midnight.

    I would assume also that the creationdate and userid columns are indexed.

    I just realized that this won't tell you all the users and their total consecutive days. But will tell you which users will have been visiting a set number of days from a date of your choosing.

    Revised solution:

    declare @days as int
    set @days = 30
    select t1.userid
    from UserHistory t1
    where (select count(1) 
           from UserHistory t3 
           where t3.userid = t1.userid
           and t3.creationdate >= DATEADD(dd, DATEDIFF(dd, 0, t1.creationdate), 0) 
           and t3.creationdate < DATEADD(dd, DATEDIFF(dd, 0, t1.creationdate) + @days, 0) 
           group by t3.userid
    ) >= @days
    group by t1.userid
    

    I've checked this and it will query for all users and all dates. It is based on Spencer's 1st (joke?) solution, but mine works.

    Update: improved the date handling in the second solution.

提交回复
热议问题