SQL to determine minimum sequential days of access?

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

    Tweaking Bill's query a bit. You might have to truncate the date before grouping to count only one login per day...

    SELECT UserId from History 
    WHERE CreationDate > ( now() - n )
    GROUP BY UserId, 
    DATEADD(dd, DATEDIFF(dd, 0, CreationDate), 0) AS TruncatedCreationDate  
    HAVING COUNT(TruncatedCreationDate) >= n
    

    EDITED to use DATEADD(dd, DATEDIFF(dd, 0, CreationDate), 0) instead of convert( char(10) , CreationDate, 101 ).

    @IDisposable I was looking to use datepart earlier but i was too lazy to look up the syntax so i figured i d use convert instead. I dint know it had a significant impact Thanks! now i know.

提交回复
热议问题