select year(vs.visitdate) year,month(vs.visitdate) month
,count(distinct vs.pid) 'case'
,count(distinct vs.visitno) 'total'
from visit vs
left join visitdiag vd on vd.visitno=vs.visitno
where vd.diagcode = 'Z13.3'
and vs.visitdate between "2012-10-01" and "2013-09-30"
group by year,month;