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 9092773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:55:09+00:00 2026-06-16T22:55:09+00:00

Each row in my table has a date time stamp, and I wish to

  • 0

Each row in my table has a date time stamp, and I wish to query the database from now, to count how many rows are in the last 30 days, the 30 days before that and so on. Until there is a 30 day bin going back to the start of the table.

I have successfully carried out this query by using Python and making several queries. But I’m almost certain that it can be done in one single MySQL query.

  • 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-06-16T22:55:10+00:00Added an answer on June 16, 2026 at 10:55 pm

    No stored procedures, temporary tables, only one query, and an efficient execution plan given an index on the date column:

    select
    
      subdate(
        '2012-12-31',
        floor(dateDiff('2012-12-31', dateStampColumn) / 30) * 30 + 30 - 1
      ) as "period starting",
    
      subdate(
        '2012-12-31',
        floor(dateDiff('2012-12-31', dateStampColumn) / 30) * 30
      ) as "period ending",
    
      count(*)
    
    from
      YOURTABLE
    group by floor(dateDiff('2012-12-31', dateStampColumn) / 30);
    

    It should be pretty obvious what is happening here, except for this incantation:

    floor(dateDiff('2012-12-31', dateStampColumn) / 30)
    

    That expression appears several times, and it evaluates to the number of 30-day periods ago dateStampColumn is. dateDiff returns the difference in days, divide it by 30 to get it in 30-day periods, and feed it all to floor() to round it to an integer. Once we have this number, we can GROUP BY it, and further we do a bit of math to translate this number back into the starting and ending dates of the period.

    Replace '2012-12-31' with now() if you prefer. Here’s some sample data:

    CREATE TABLE YOURTABLE
        (`Id` int, `dateStampColumn` datetime);
    
    INSERT INTO YOURTABLE
        (`Id`, `dateStampColumn`)
    VALUES
        (1, '2012-10-15 02:00:00'),
        (1, '2012-10-17 02:00:00'),
        (1, '2012-10-30 02:00:00'),
        (1, '2012-10-31 02:00:00'),
        (1, '2012-11-01 02:00:00'),
        (1, '2012-11-02 02:00:00'),
        (1, '2012-11-18 02:00:00'),
        (1, '2012-11-19 02:00:00'),
        (1, '2012-11-21 02:00:00'),
        (1, '2012-11-25 02:00:00'),
        (1, '2012-11-25 02:00:00'),
        (1, '2012-11-26 02:00:00'),
        (1, '2012-11-26 02:00:00'),
        (1, '2012-11-24 02:00:00'),
        (1, '2012-11-23 02:00:00'),
        (1, '2012-11-28 02:00:00'),
        (1, '2012-11-29 02:00:00'),
        (1, '2012-11-30 02:00:00'),
        (1, '2012-12-01 02:00:00'),
        (1, '2012-12-02 02:00:00'),
        (1, '2012-12-15 02:00:00'),
        (1, '2012-12-17 02:00:00'),
        (1, '2012-12-18 02:00:00'),
        (1, '2012-12-19 02:00:00'),
        (1, '2012-12-21 02:00:00'),
        (1, '2012-12-25 02:00:00'),
        (1, '2012-12-25 02:00:00'),
        (1, '2012-12-26 02:00:00'),
        (1, '2012-12-26 02:00:00'),
        (1, '2012-12-24 02:00:00'),
        (1, '2012-12-23 02:00:00'),
        (1, '2012-12-31 02:00:00'),
        (1, '2012-12-30 02:00:00'),
        (1, '2012-12-28 02:00:00'),
        (1, '2012-12-28 02:00:00'),
        (1, '2012-12-30 02:00:00');
    

    And the result:

    period starting     period ending   count(*)
    2012-12-02          2012-12-31      17
    2012-11-02          2012-12-01      14
    2012-10-03          2012-11-01      5
    

    period endpoints are inclusive.

    Play with this in SQL Fiddle.

    There’s a bit of potential goofiness in that any 30 day period with zero matching rows will not be included in the result. If you could join this against a table of periods, that could be eliminated. However, MySQL doesn’t have anything like PostgreSQL’s generate_series(), so you’d have to deal with it in your application or try this clever hack.

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

Sidebar

Related Questions

I have a table with many rows and each row has select lists. Something
I have this kind of table, it has many rows and each row has
I have a table where each row has a start and stop date-time. These
My mysql database has a table with several hundred thousand rows. Each row has
I have a table with 3 rows. Each row has 3 td. The first
I've a table with more than 5000 rows. Each row has a category, and
I have a massive table with thousands of rows. Each row has a unique
I have a tableview with custom UITableViewCells, each row in the table has a
I have a table where each row has a description field as well as
I have a table where each row has 13 TD elements. I want to

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.