SELECT person.pid,concat(titlename,fname,' ',lname) as name,getAgeYearNum(birth,current_date) as age,
person.idcard,
person.birth,
CASE WHEN person.candobedhomesocial = '1' THEN 1 ELSE '' END AS 'ติดสังคม',
CASE WHEN person.candobedhomesocial = '2' THEN 1 ELSE '' END AS 'ติดบ้าน',
CASE WHEN person.candobedhomesocial = '3' THEN 1 ELSE '' END AS 'ติดเตียง',
villno,
house.hno
,villname
from person
LEFT JOIN ctitle on person.prename = ctitle.titlecode
INNER JOIN house on person.pcucodeperson = house.pcucodeperson and person.hcode = house.hcode
LEFT JOIN persondeath ON person.pid = persondeath.pid
INNER JOIN village on house.villcode = village.villcode and villno <>'00'
WHERE getAgeYearNum (person.birth,current_date) >= 60 
and nation = 99 
and person.typelive in ('1','3') 
AND persondeath.pid IS NULL
GROUP BY villno,hno,person.pid;