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

  • Home
  • SEARCH
  • 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 192743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:27:05+00:00 2026-05-11T16:27:05+00:00

I have user login data with timestamps and what I would like to do

  • 0

I have user login data with timestamps and what I would like to do is get a histogram of logins by year, but with the year starting at an arbitrary date. For example, I want the following sort of information:

1 May 2005 - 30 Apr 2006 | 525
1 May 2006 - 30 Apr 2007 | 673
1 May 2007 - 30 Apr 2008 | 892
1 May 2006 - 30 Apr 2009 | 1047

The labels in the first column are not important, but the date ranges are. I know I can break it down by strait years with:

SELECT YEAR([date]) AS [year], COUNT(*) AS cnt 
FROM logins
GROUP BY YEAR([date])
ORDER BY [year]

But that doesn’t give me the data ranges I want. How can this be done?

  • 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-11T16:27:05+00:00Added an answer on May 11, 2026 at 4:27 pm
    declare @baseDate datetime
    set @baseDate = '1 May 2005'
    
    SELECT
        datediff(year, @baseDate, [date]) AS YearBucket 
        ,COUNT(*) AS cnt 
    FROM logins
    GROUP BY datediff(year, @baseDate, [date])
    ORDER BY datediff(year, @baseDate, [date])
    

    EDIT – apologies, you are correct. Here is a fixed version (I should have used a test table to start with…)

    create table logins (date datetime, foo int)
    insert logins values ('1 may 2005', 1)
    insert logins values ('1 apr 2006', 2)
    insert logins values ('1 may 2006', 3)
    
    declare @baseDate datetime
    set @baseDate = '1 May 2005'
    
    SELECT
        datediff(day, @baseDate, [date]) / 365 AS YearBucket 
        ,COUNT(*) AS cnt 
    FROM logins
    GROUP BY datediff(day, @baseDate, [date]) / 365
    ORDER BY datediff(day, @baseDate, [date]) / 365
    

    Change the datediff units if you want more granularity than days.

    EDIT #2 – ok, here is a more robust solution that handles leap years 🙂
    EDIT #3 – Actually this doesn’t handle leap years, instead it allows for variable intervals of time to be specified. Go with dateadd(year, 1, @baseDate) for the leap year safe approach.

    declare @baseDate datetime, @interval datetime
    --@interval is expressed as time above 0 time (1/1/1900)
    select @baseDate = '1 May 2005', @interval = '1901'
    
    declare @timeRanges table (beginIntervalInclusive datetime, endIntervalExclusive datetime)
    declare @i int
    set @i = 1
    while @i <= 10
    begin
        insert @timeRanges values(@baseDate, @baseDate + @interval)
        set @baseDate = @baseDate + @interval
        set @i = @i + 1
    end
    
    SELECT
        tr.beginIntervalInclusive,
        tr.endIntervalExclusive,
        COUNT(*) AS cnt 
    FROM logins join @timeRanges as tr
        on logins.date >= tr.beginIntervalInclusive
            and logins.date < tr.endIntervalExclusive
    GROUP BY  tr.beginIntervalInclusive, tr.endIntervalExclusive
    ORDER BY  tr.beginIntervalInclusive
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single MySQL table with login data of users. user | date
I have a web-based system built with user login data based on session variables.
I have two sql tables for user login, store and retrieve data according to
I'm just starting to learn AspectJ, and I have use-case for say, User login.
I have the following code: <?php if (!$user) { ?> <div class=fb-login-button data-perms=email,user_birthday,publish_stream>Login with
i have a login form: <form method =POST action=/login.php> ... </form> i would like
Upon user login I check their roles. If they have no roles I want
I am using mysql and have a table (Log_Table) storing user login dates, a
I have 2 site: example.com and exampletwo.com I want that when a user login
I have my registerUsers.html as below <html> <head> <title>userlogin</title> </head> <body> <h1><center>User login</center></h1> <p>

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.