Looking to get record counts for each month. However, several months has no records therefore no row is returned. How can I get a count of 0 for that month?
select months, count(rowid) as counter from (
select to_char(date_entered, 'MM') as months
from mydatatable
where to_char(date_entered, 'yyyy') = '2011'
)
group by months
order by months
Result:
Month Count
01 32
03 12
04 11
06 10
07 222
08 32
Even tried playing with subq select 1,2,3,4,5,6,7,8,9,10,11,12 from dual and could not get it to work. No pivot capability yet… ;(
You’re probably better off storing the 01 to 12 in a table, but the general approach is to use a left join:
Update used a more index friendly way of restricting the year.
http://sqlfiddle.com/#!4/68085/10