SELECT village.villno,village.villname,
count(distinct person.pid) AS target,COUNT( case when visitepi = 'DTC'
and visitepi.dateepi BETWEEN "2015-01-01" and "2015-04-30"  as total,
concat(round(count( case when visitepi.dateepi BETWEEN "2015-01-01" and "2015-04-30"and visitepi.vaccinecode = "DTC" then visitepi.pid = person.pid
else null end)*100/ count( distinct case when ((left(birth,10)) between '1965-01-01' and '1995-12-31')
then person.pid else null end),2),' %') as 'PP'
,COUNT( case when visitepi.vaccinecode = "DTC"
and visitepi.dateepi BETWEEN "2015-01-01" and "2015-01-31" then visitepi.pid = person.pid else null end)as 01_2558
,COUNT( case when visitepi.vaccinecode = "DTC"
and visitepi.dateepi BETWEEN "2015-02-01" and "2015-02-28" then visitepi.pid = person.pid else null end)as 02_2558
,COUNT( case when visitepi.vaccinecode = "DTC"
and visitepi.dateepi BETWEEN "2015-03-01" and "2015-03-31" then visitepi.pid = person.pid else null end)as 03_2558
,COUNT( case when visitepi.vaccinecode = "DTC"
and visitepi.dateepi BETWEEN "2015-04-01" and "2015-04-30" then visitepi.pid = person.pid else null end)as 04_2558
FROM person
INNER JOIN house ON person.hcode= house.hcode
INNER JOIN village ON village.villcode = house.villcode
left JOIN visitepi ON visitepi.pid = person.pid
AND visitepi.pcucodeperson=person.pcucodeperson
#left JOIN ovst_vaccine ON ovst_vaccine.vn = ovstdiag.vn
#left JOIN person_vaccine ON person_vaccine.person_vaccine_id = ovst_vaccine.person_vaccine_id
WHERE ((left(birthdate,10)) between '1965-01-01' and '1995-12-31') AND
person.house_regist_type_id IN (1,3) AND
person.person_discharge_id<> 1 AND person.death ='N' and
village.village_moo <> 0 AND person.sex IN (1,2) GROUP BY village.village_id 
UNION
SELECT ' ','total',
count(distinct person.person_id) AS target,COUNT( case when person_vaccine.export_vaccine_code = "901"
and ovstdiag.vstdate BETWEEN "2015-01-01" and "2015-04-30" then ovstdiag.hn = person.patient_hn else null end)as total,
concat(round(count( case when ovstdiag.vstdate BETWEEN "2015-01-01" and "2015-04-30"and person_vaccine.export_vaccine_code = "901" then ovstdiag.hn = person.patient_hn
else null end)*100/ count( distinct case when ((left(birthdate,10)) between '1965-01-01' and '1995-12-31')
then person.person_id else null end),2),' %') as 'PP'
,COUNT( case when person_vaccine.export_vaccine_code = "901"
and ovstdiag.vstdate BETWEEN "2015-01-01" and "2015-01-31" then ovstdiag.hn = person.patient_hn else null end)as 01_2558
,COUNT( case when person_vaccine.export_vaccine_code = "901"
and ovstdiag.vstdate BETWEEN "2015-02-01" and "2015-02-28" then ovstdiag.hn = person.patient_hn else null end)as 02_2558
,COUNT( case when person_vaccine.export_vaccine_code = "901"
and ovstdiag.vstdate BETWEEN "2015-03-01" and "2015-03-31" then ovstdiag.hn = person.patient_hn else null end)as 03_2558
,COUNT( case when person_vaccine.export_vaccine_code = "901"
and ovstdiag.vstdate BETWEEN "2015-04-01" and "2015-04-30" then ovstdiag.hn = person.patient_hn else null end)as 04_2558
FROM person INNER JOIN house ON person.house_id = house.house_id
INNER JOIN village ON village.village_id = house.village_id
left JOIN ovstdiag ON ovstdiag.hn = person.patient_hn
left JOIN ovst_vaccine ON ovst_vaccine.vn = ovstdiag.vn
left JOIN person_vaccine ON person_vaccine.person_vaccine_id = ovst_vaccine.person_vaccine_id
WHERE ((left(birthdate,10)) between '1965-01-01' and '1995-12-31') AND
person.house_regist_type_id IN (1,3) AND
person.person_discharge_id<> 1 AND person.death ='N' and
village.village_moo <> 0 AND person.sex IN (1,2) GROUP BY person.patient_link