Jeff recently asked this question and got some great answers.
Jeff's problem revolved around finding the users that have had (n) consecutive days where they have logged into a system. Using a database table structure as follows:
Id UserId CreationDate ------ ------ ------------ 750997 12 2009-07-07 18:42:20.723 750998 15 2009-07-07 18:42:20.927 751000 19 2009-07-07 18:42:22.283
Read the original question first for clarity and then...
I was intrigued by the problem of determining how many distinct (n)-day periods for a user.
Could one craft a speedy SQL query that could return a list of users and the number of distinct (n)-day periods they have?
EDIT: as per a comment below If someone has 2 consecutive days, then a gap, then 4 consecutive days, then a gap, then 8 consecutive days. It would be 3 "distinct 4 day periods". The 8 day period should count as two back-to-back 4 day periods.