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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:11:07+00:00 2026-06-10T00:11:07+00:00

i am trying to group days between two date filters by month. Is it

  • 0

i am trying to group days between two date filters by month. Is it possible to do this in mysql.

Example : StartDate : 2012-01-19  EndDate: 2012-03-24

The query should return days grouped by month

Jan : 19
Feb  : 29
Mar :24
Apr : 0
May : 0 etc

Is there a way to do this?

Thanks!

  • 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-10T00:11:09+00:00Added an answer on June 10, 2026 at 12:11 am

    this query will work for you for a limited date range(past 1,00,000 days). but better way would be to write a separate function or procedure with WHILE loop.

    SELECT DATE_FORMAT(_date, '%M') AS month,
           COUNT(1) AS days
    FROM (
            SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY AS _date
            FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS d
            CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS e
         ) a
    WHERE _date BETWEEN '2012-01-19' AND '2012-03-24'
    GROUP BY MONTH(_date)
    ORDER BY MONTH(_date);
    

    New answer for your question in comment: just had a question, how can I do a JOIN to another table using this query. The basic idea is I have a Bookings table which has a startDate and an endDate, and using these as parameters for the _date BETWEEN , I need to find out the number of days for each month. Any suggestions on how I could do that? Also is it possible to return the results in a single row rather than multiple rows:

    see SQL FIDDLE DEMO HERE:

    CREATE TABLE Bookings (
      start_date date DEFAULT NULL,
      end_date date DEFAULT NULL
    );
    INSERT INTO Bookings(start_date, end_date)VALUES('2012-01-19','2012-03-24'),('2012-01-05','2012-08-21');
    
    SELECT b.*,
    (
     SELECT CONCAT(DATE_FORMAT(b.start_date, '%M:'), (DATEDIFF(LAST_DAY(b.start_date), b.start_date) + 1), ',',
                   GROUP_CONCAT(CONCAT(DATE_FORMAT(first_day, '%M:'),days)), ',',
                   DATE_FORMAT(b.end_date, '%M:%d')
                  ) AS total_days
    FROM(
                 SELECT DATE_FORMAT(_date, '%Y-%m-01') AS first_day,
                        COUNT(1) AS days
                 FROM (
                        SELECT CURDATE() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a)) DAY AS _date
                        FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
                        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
                        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
                        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS d
                        CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS e
                     ) a, (SELECT @cnt := 0) b
                  GROUP BY YEAR(_date), MONTH(_date)
                 ) a
                 WHERE first_day BETWEEN DATE_ADD(LAST_DAY(b.start_date), INTERVAL 1 DAY) AND DATE_SUB(b.end_date, INTERVAL 1 MONTH)
            ) total_days
    FROM Bookings b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been pulling my hair out for two days trying to put a MySQL
I've spent the last two days trying to find a solution to this problem
I'm trying to group some sets in disjoint sets. For example, if I have
I have been trying for days to figure out how to parse this JSON
I am trying to sort articles by Today, This Week, This Month , All
For few days now I'm trying to solve this problem. I have table group_user,
I've been trying to get this complex MYSQL query to work exactly right over
I've spent a couple of days trying to solve this with limited success, I'm
For two days I have been trying to add new customer attribute to the
I'm trying to group together radiobuttons that are creating using a for loop and

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.