select a.aid,ifnull(t.full_name,if(left(a.aid,2)='62','ในจังหวัด','จังหวัดอื่น')) as name
,count(distinct a.hn) as 'case',count(a.hn) as 'total'
,sum(count_in_month=0) as new_month
,sum(count_in_year=0) as new_year
from (select v.vn,v.hn,count_in_year,count_in_month
,if(left(v.aid,4)='6210',v.aid
,if(left(v.aid,2)='62','629999','999999')) as aid
from vn_stat v
where v.vstdate between "2009-07-01" and "2009-07-31"
and v.age_y>=40 ) a
left join thaiaddress t on a.aid=t.addressid
group by a.aid
order by a.aid