SELECT
concat(ctitle.titlename,person.fname," ",person.lname)as'ชื่อ-สกุล',
house.hno as ที่อยู่,
village.villno as หมู่,
village.villname as บ้าน,
person.typelive,
CONCAT(DATE_FORMAT(person.birth,'%d/%m/'),DATE_FORMAT(person.birth,'%Y')+543)as วันเกิด ,
getagemonth(person.birth,CURDATE() )as อายุเดือน
FROM person
INNER JOIN house on person.hcode=house.hcode AND person.pcucodeperson=house.pcucodeperson
INNER JOIN village on house.villcode = village.villcode and house.pcucode = village.pcucode
INNER JOIN ctitle on person.prename=ctitle.titlecode
WHERE person.typelive in ('0','1','3') and right(house.villcode,2)!='00' and person.birth between '20080601' and '20120131' 
and getagemonth(person.birth,CURDATE() ) <= 84 
AND CONCAT(person.pid,person.pcucodeperson) 
NOT IN (SELECT CONCAT(persondeath.pid,persondeath.pcucodeperson)
FROM persondeath 
WHERE deaddate <= CURDATE() or deaddate is not NULL) 
AND ((person.dischargetype is null) OR (person.dischargetype != '1')) # คัดคนตายออกไป
GROUP BY person.birth
ORDER BY village.villno;