1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

Count entries per month?

Discussion in 'SQL - Questions and Answers' started by Gary Bennion, Jan 30, 2007.

  1. Gary Bennion Guest

    Dialect: MySQL
    I have a table with a date_created column in it. I want to count the number of entries for every month. i.e
    Code:
    ----------|------------
    jan 2006  | 12
    feb 2006  | 23
    
    Hope you can help.
  2. Gary Bennion Guest

    Dialect: MySQL
    Guys, I have found the anwer myself, put though i would post it incase its of interest to anyone else:

    SQL:

    SELECT MONTH(date_created), YEAR(date_created), COUNT(*)
    FROM contact_requests
    GROUP BY MONTH(date_created), YEAR(date_created)
  3. meera Guest

    Dialect: MySQL
    SQL:

    SELECT monthname(date_created),year(date_created),count(*) FROM table
  4. Aneymeyr Guest

    Actually, Gary Bennion, the group by should be:

    GROUP BY YEAR(date_created), MONTH(date_created)

    Otherwise you'll get unexpected results... (lots of things grouped in particular months, for example, not taking into account their proper year).

    Cheers!

    Amy
  5. milund New Member

    How would I do this if I wanted the same query between to dates and I also wanted a result count of 0 for months that does not have any entries?

Share This Page