sql - Month statistics -


i have table in postgres , mysql 'created_at' column. query following:

month count 1     0 2     0 3     0 4     12 5     15 ... 

can cough sql? notice months no rows returned must listed 0's. have this:

select month(created_at) month, count(*) c  `sale_registrations`  (created_at>='2011-01-01' , created_at<='2011-12-31')  group month(created_at)  order month(created_at) 

use extract(month created_at) month. works in mysql well.

edit: use right join on table month numbers:

create table months(nr tinyint); insert months(nr) values (1),(3),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);  select    nr month,    count(*) c     sale_registrations      right join months on extract(month created_at) = nr    (created_at between '2011-01-01' , '2011-12-31')  group    extract(month created_at) order    extract(month created_at) asc; 

in postgresql use generate_series(), that's not going work in mysql.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -