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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

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