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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:13:52+00:00 2026-06-11T21:13:52+00:00

I’m trying to count how many result in each month. This is my query

  • 0

I’m trying to count how many result in each month.

This is my query :

SELECT 
    COUNT(*) as nb,
    CONCAT(MONTH(t.date),0x3a,YEAR(t.date)) as period
FROM table1 t
WHERE t.criteria = 'value'
GROUP BY MONTH(t.date)
ORDER BY YEAR(t.date)

My Result:

nb  period
---------------
7   6:2009
46  8:2009
2   10:2009
1   11:2009
14  1:2009
9   9:2010
161 7:2010
5   2:2010
88  3:2010
28  4:2010
4   5:2011
2   12:2011

The problem is, I’m sure that I’ve result between 5:2011 & 12:2011 , and each other period
since 2009 … :/

This is a problem of my request or mysql configuration ?

Thx a lot

  • 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-11T21:13:53+00:00Added an answer on June 11, 2026 at 9:13 pm

    You have to group by both the year and the month. Otherwise your April 2012 rows are grouped with April 2011 (and April 2010 …) rows as well.

    SELECT 
        COUNT(*) AS nb,
        CONCAT(MONTH(t.date), ':', YEAR(t.date)) AS period
    FROM table1 AS t
    WHERE t.criteria = 'value'
    GROUP BY YEAR(t.date)
           , MONTH(t.date) ;
    

    (and is there a reason you used 0x3a and not ':'?)


    You could also use some other DATE and TIME functions of MySQL so there are fewer functions calls per row and probably a more efficient query:

    SELECT 
        COUNT(*) AS nb,
        DATE_FORMAT(t.date, '%m:%Y') AS period
    FROM table1 AS t
    WHERE t.criteria = 'value'
    GROUP BY EXTRACT( YEAR_MONTH FROM t.date) ;
    

    For several queries, it’s useful to have a permanent Calendar table in your database (with all dates or all year-months) or even several Calendar tables. Example:

    CREATE TABLE CalendarYear
      ( Year SMALLINT UNSIGNED NOT NULL
      , PRIMARY KEY (Year)
      ) ENGINE = InnoDB ;
    
    INSERT INTO CalendarYear
      (Year)
    VALUES
      (1900), (1901), ..., (2099) ;
    
    CREATE TABLE CalendarMonth
      ( Month TINYINT UNSIGNED NOT NULL
      , PRIMARY KEY (Month)
      ) ENGINE = InnoDB ;
    
    INSERT INTO CalendarMonth
      (Month)
    VALUES
      (1), (2), ..., (12) ;
    

    Those can also help us make the one we’ll need here:

    CREATE TABLE CalendarYearMonth
      ( Year SMALLINT UNSIGNED NOT NULL
      , Month TINYINT UNSIGNED NOT NULL
      , FirstDay DATE NOT NULL
      , NextMonth_FirstDay DATE NOT NULL
      , PRIMARY KEY (Year, Month)
      ) ENGINE = InnoDB ;
    
    INSERT INTO CalendarYearMonth
      (Year, Month, FirstDay, NextMonth_FirstDay)
    SELECT
        y.Year
      , m.Month
      , MAKEDATE(y.Year, 1) + INTERVAL (m.Month-1) MONTH
      , MAKEDATE(y.Year, 1) + INTERVAL (m.Month) MONTH
    FROM
        CalendarYear AS y
      CROSS JOIN
        CalendarMonth AS m ;
    

    Then you can use the Calendar tables to write more complex queries, like the variation you want (with missing months) and probably more efficiently. Tested in SQL-Fiddle:

    SELECT 
        COUNT(t.date) AS nb,
        CONCAT(cal.Month, ':', cal.Year) AS period
    FROM
            CalendarYearMonth AS cal
        JOIN
            ( SELECT
                  YEAR(MIN(date))  AS min_year
                , MONTH(MIN(date)) AS min_month
                , YEAR(MAX(date))  AS max_year
                , MONTH(MAX(date)) AS max_month
              FROM table1
              WHERE criteria = 'value'
            ) AS mm
          ON  (cal.Year, cal.Month) >= (mm.min_year, mm.min_month)
          AND (cal.Year, cal.Month) <= (mm.max_year, mm.max_month)
        LEFT JOIN
            table1 AS t
          ON  t.criteria = 'value'
          AND t.date >= cal.FirstDay
          AND t.date < cal.NextMonth_FirstDay
    GROUP BY 
        cal.Year, cal.Month ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.