这个sql怎么写?

表1:
id name adate
1 n1 2011.3.11
2 n2 2011.4.12
3 n3 2011.3.21
期望统计结果:
月份 数量
1 0
2 0
3 2
4 1
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0

作者: ashyrabbit   发布时间: 2011-12-25

with tempyeartable as
(
select '01' as month,'2011-01' as yymonth from dual
union
select '02' as month,'2011-02' as yymonth from dual
union
select '03' as month,'2011-03' as yymonth from dual
union
select '04' as month,'2011-04' as yymonth from dual
union
select '05' as month,'2011-05' as yymonth from dual
union
select '06' as month,'2011-06' as yymonth from dual
union
select '07' as month,'2011-07' as yymonth from dual
union
select '08' as month,'2011-08' as yymonth from dual
union
select '09' as month,'2011-09' as yymonth from dual
union
select '10' as month,'2011-10' as yymonth from dual
union
select '11' as month,'2011-11' as yymonth from dual
union
select '12' as month,'2011-12' as yymonth from dual
),
selecttable as
(
select to_char(adate,'yyyy-mm') t_mm,count(*) num
from 表1
group by to_char(adate,'yyyy-mm')
)
select b.month,nvl(a.num,0)
from selecttable a,tempyeartable b
where a.t_mm(+) = b.yymonth
order by month

作者: zxf261   发布时间: 2011-12-26