Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 292371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:10:27+00:00 2026-05-12T06:10:27+00:00

The following User History table contains one record for every day a given user

  • 0

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 records, but only one record per day per user. If the user has not accessed the website for that day, no record will be generated.

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

What I’m looking for is a SQL query on this table with good performance, that tells me which userids have accessed the website for (n) continuous days without missing a day.

In other words, how many users have (n) records in this table with sequential (day-before, or day-after) dates? If any day is missing from the sequence, the sequence is broken and should restart again at 1; we’re looking for users who have achieved a continuous number of days here with no gaps.

Any resemblance between this query and a particular Stack Overflow badge is purely coincidental, of course.. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:10:27+00:00Added an answer on May 12, 2026 at 6:10 am

    The answer is obviously:

    SELECT DISTINCT UserId
    FROM UserHistory uh1
    WHERE (
           SELECT COUNT(*) 
           FROM UserHistory uh2 
           WHERE uh2.CreationDate 
           BETWEEN uh1.CreationDate AND DATEADD(d, @days, uh1.CreationDate)
          ) = @days OR UserId = 52551
    

    EDIT:

    Okay here’s my serious answer:

    DECLARE @days int
    DECLARE @seconds bigint
    SET @days = 30
    SET @seconds = (@days * 24 * 60 * 60) - 1
    SELECT DISTINCT UserId
    FROM (
        SELECT uh1.UserId, Count(uh1.Id) as Conseq
        FROM UserHistory uh1
        INNER JOIN UserHistory uh2 ON uh2.CreationDate 
            BETWEEN uh1.CreationDate AND 
                DATEADD(s, @seconds, DATEADD(dd, DATEDIFF(dd, 0, uh1.CreationDate), 0))
            AND uh1.UserId = uh2.UserId
        GROUP BY uh1.Id, uh1.UserId
        ) as Tbl
    WHERE Conseq >= @days
    

    EDIT:

    [Jeff Atwood] This is a great fast solution and deserves to be accepted, but Rob Farley’s solution is also excellent and arguably even faster (!). Please check it out too!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a following structure: User has many books in his history which was
I have the following domains: User, Role, Company. User and Role has m:n relationship,
Given the following models: User.rb NetworkDepartment.rb (id, title,added_by) belongs_to :user, :foreign_key => :added_by I
Following class structure is given: class Job { String description; Collection<JobHistory> history; } class
My schema has the following relations: User hasMany Transaction belongsTo User Item hasMany Transaction
I have the following user-control: <%@ Control Language=C# AutoEventWireup=true CodeFile=FadingMessage.ascx.cs Inherits=includes_FadingMessage %> <asp:PlaceHolder Visible=false
I have the following user control that is embedded within another user control. <UserControl.Resources>
I have the following user resource: class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name
I have following user control: <UserControl.Resources> <Style TargetType=HeaderedItemsControl> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=HeaderedItemsControl> <Grid>
I am looking for scalable way to do the following: User login Fetch all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.