SELECT *
FROM(SELECT CONCAT(ctitle.titlename,person.fname," ",person.lname) AS "ชื่อ-สกุล"
,DATE_FORMAT(person.birth,'%Y-%m-%d') AS 'pbirth'
,CONCAT(DATE_FORMAT(person.birth,'%d/%m/'),DATE_FORMAT(person.birth,'%Y')+543) AS วันเกิด
,person.idcard as 'เลขบัตรประชาชน'
,house.hno AS 'บ้านเลขที่'
,RIGHT(house.villcode,2) AS 'หมู่ที่'
,village.villname
,getAgeYearNum(person.birth,CURRENT_DATE()) AS 'อายุ'
,CONCAT(p1.fname," ",p1.lname) AS 'อสม'
,user.fullname AS 'หมอประจำบ้าน'
,CASE WHEN getAgeYearNum(person.birth,CURDATE()) BETWEEN 25 AND 59 THEN '/' ELSE '' END AS 'W'
,CASE WHEN getAgeYearNum(person.birth,CURDATE()) BETWEEN 3 AND 12 THEN '/' ELSE '' END AS 'E'
,CASE WHEN getAgeYearNum(person.birth,CURDATE())<= 5 THEN '/' ELSE" " END AS 'Child'
,CASE WHEN CONCAT(person.pcucodeperson,person.pid) IN
(SELECT CONCAT(visitanc.pcucodeperson,visitanc.pid )
FROM visitanc 
WHERE visitanc.datecheck BETWEEN '2014-10-01'AND '2015-09-30' ) THEN '/' ELSE" " END AS 'ANC'
,CASE WHEN CONCAT(person.pcucodeperson,person.pid) IN
(SELECT CONCAT(personchronic.pcucodeperson,personchronic.pid )
FROM personchronic 
WHERE personchronic.typedischart='03' ) THEN '/' ELSE" " END AS 'NCD'
,CASE WHEN CONCAT(person.pcucodeperson,person.pid)IN
(SELECT CONCAT(personunable.pcucodeperson,personunable.pid )
FROM personunable ) THEN '/' ELSE" " END AS 'D'
,CASE WHEN getAgeYearNum(person.birth,CURDATE())>= 60 THEN '/' ELSE" " END AS 'Old',curdate() cdate
FROM person
INNER JOIN ctitle ON person.prename=ctitle.titlecode
INNER JOIN house ON person.pcucodeperson=house.pcucodeperson AND person.hcode=house.hcode
INNER JOIN village ON house.villcode=village.villcode AND house.pcucode=village.pcucode
LEFT JOIN person AS p1 ON house.pidvola = p1.pid AND house.pcucodepersonvola = p1.pcucodeperson
LEFT JOIN personchronic ON person.pcucodeperson=personchronic.pcucodeperson AND person.pid=personchronic.pid
LEFT JOIN `user` ON house.usernamedoc= user.username
WHERE CONCAT(person.pcucodeperson,person.pid) not in 
(SELECT CONCAT(persondeath.pcucodeperson,persondeath.pid)
FROM persondeath)
AND RIGHT(house.villcode,2)!='00'
AND person.typelive IN ('1','3')
GROUP BY person.pid
ORDER BY p1.pid,getAgeYearNum(person.birth,'2014-10-01')) AS cando
WHERE cando.child ='/' OR cando.ANC='/' OR cando.NCD='/' OR cando.D='/' OR cando.old='/';