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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:03:06+00:00 2026-05-20T05:03:06+00:00

I have a table in Postgres and MySQL with a ‘created_at’ column. I would

  • 0

I have a table in Postgres and MySQL with a ‘created_at’ column. I would like to query it for the following:

Month Count
1     0
2     0
3     0
4     12
5     15
...

Can anyone cough up some sql? Notice that the months with no rows returned must be listed as 0’s. I have this:

SELECT month(created_at) as month, count(*) as c 
FROM `sale_registrations` 
WHERE (created_at>='2011-01-01' and created_at<='2011-12-31') 
GROUP BY month(created_at) 
ORDER BY month(created_at)
  • 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-20T05:03:07+00:00Added an answer on May 20, 2026 at 5:03 am

    Use EXTRACT(month FROM created_at) to get the month. This works in MySQL as well.

    Edit: Use a RIGHT JOIN on a table with the month numbers:

    CREATE TABLE months(nr tinyint);
    INSERT INTO months(nr) VALUES (1),(3),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);
    
    SELECT 
      nr as month, 
      COUNT(*) as c 
    FROM 
      sale_registrations 
        RIGHT JOIN months ON EXTRACT(month FROM created_at) = nr
    WHERE 
      (created_at BETWEEN '2011-01-01' AND '2011-12-31') 
    GROUP BY 
      EXTRACT(month FROM created_at)
    ORDER BY 
      EXTRACT(month FROM created_at) ASC;
    

    In PostgreSQL you could use generate_series(), but that’s not going to work in MySQL.

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

Sidebar

Related Questions

I have a postgres table that looks in part like: Year | Month |
I have table that I insert data with following query (from c# code): INSERT
Please help me to write the following sql. I have a table like this,
I have designed a table (in postgres) where 'id' column is autoincremented via its
I have a Postgres table with more than 8 million rows. Given the following
I have a postgres table with a column (called id) which contains strings. In
I have a postgres table like this: CREATE SEQUENCE seq; CREATE TABLE tbl (id
I have the following table declared in MySQL: CREATE TABLE IF NOT EXISTS `ci_sessions`
I have a postgres table that has the following fields start_date,duration duration contains any
I have the following table in postgres: CREATE TABLE test ( id serial NOT

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.