SELECT
' ' AS `ลำดับที่`,
p.pid AS Hn,
CONCAT(c.titlename,p.fname,' ',p.lname) AS `ชื่อสกุล`,
concat(substr(p.birth,9,2),'/',substr(p.birth,6,2),'/',substr(p.birth,1,4)+543) AS `วันเกิด`,
getAgeYearNum(p.birth,CURDATE()) AS `อายุ`,
h.hno AS `บ้านเลขที่`,
v.villno AS `หมู่ที่`
FROM
person AS p
INNER JOIN house AS h ON p.pcucodeperson = h.pcucode AND p.hcode = h.hcode
INNER JOIN village AS v ON h.pcucode = v.pcucode AND h.villcode = v.villcode
INNER JOIN ctitle AS c ON p.prename = c.titlecode
WHERE
getAgeYearNum(p.birth,CURDATE()) > 64
AND p.typelive IN (0,1,3)
AND p.dischargetype = 9
AND p.pid NOT IN (SELECT persondeath.pid FROM persondeath)
AND p.pid NOT IN (SELECT personchronic.pid FROM personchronic)
GROUP BY
p.pid
ORDER BY
v.villno;