SQL to determine minimum sequential days of access?

前端 未结 19 1722
我在风中等你
我在风中等你 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:24

    A couple of SQL Server 2012 options (assuming N=100 below).

    ;WITH T(UserID, NRowsPrevious)
         AS (SELECT UserID,
                    DATEDIFF(DAY, 
                            LAG(CreationDate, 100) 
                                OVER 
                                    (PARTITION BY UserID 
                                         ORDER BY CreationDate), 
                             CreationDate)
             FROM   UserHistory)
    SELECT DISTINCT UserID
    FROM   T
    WHERE  NRowsPrevious = 100 
    

    Though with my sample data the following worked out more efficient

    ;WITH U
             AS (SELECT DISTINCT UserId
                 FROM   UserHistory) /*Ideally replace with Users table*/
        SELECT UserId
        FROM   U
               CROSS APPLY (SELECT TOP 1 *
                            FROM   (SELECT 
                                           DATEDIFF(DAY, 
                                                    LAG(CreationDate, 100) 
                                                      OVER 
                                                       (ORDER BY CreationDate), 
                                                     CreationDate)
                                    FROM   UserHistory UH
                                    WHERE  U.UserId = UH.UserID) T(NRowsPrevious)
                            WHERE  NRowsPrevious = 100) O
    

    Both rely on the constraint stated in the question that there is at most one record per day per user.

提交回复
热议问题