title: SQL query to return count of monthly rows is slow
body: <p>So, the original query was quite simple.</p>
  
  <pre><code>select MONTH(DateColumn), COUNT(DateColumn)
  from myTable
  where YEAR(DateColumn) = '2014'
  group by MONTH(DateColumn)
  order by MONTH(DateColumn)
  </code></pre>
  
  <p>But, one of the months didn't have data, and I need to have all 12 months covered. So I'm busy reworking it.. I have a solution, but it is painfully slow. There must be a better way.</p>
  
  <pre><code>select o.MonthCount, coalesce(d.total,0) from (
  select top 12 ROW_NUMBER() over (order by (select 12)) as MonthCount from myTable
  ) o outer apply (
  select month(e.DateColumn) as mon, COUNT(e.DateColumn) as total
  from myTable e 
  where YEAR(e.DateColumn) = '2013' 
  and o.monthcount = month(e.DateColumn) 
  group by MONTH(e.DateColumn) 
  ) d 
  </code></pre>