sql - LEFT OUTER JOIN not returning NULL values -


i have pretty simple sample query trying complete.

sql fiddle

select     month(s.report_date),     coalesce(count(*),0)     stat_summary s left outer join ref_months m on month(s.report_date) = m.month_id group month(s.report_date) 

my results like:

enter image description here

my desired results like:

month  | count ----------------     1  | 0     2  | 0     3  | 0     4  | 0     5  | 0     6  | 0     7  | 0     8  | 0     9  | 4     10 | 9     11 | 0     12 | 0 

you need use months table primary one:

select     m.month_id,     coalesce(count(s.report_date),0) ref_months m  left join stat_summary s      on month(s.report_date) = m.month_id group m.month_id; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -