set @d='2014-12-01';
set @d2='2014-12-01';
SELECT
person.pid AS Hn,
ctitle.titlename,
person.fname as 'ชื่อ',
person.lname as 'นามสกุล',
concat(date_format(person.birth,"%d"),'-',date_format(person.birth,"%m"),'-',date_format(person.birth,"%Y")+543) as วันเดือนปีเกิด,
getAgeYearNum(person.birth,@d2) AS age,
house.hno, right((house.villcode),2) AS moo,
village.villname,
round(DATEDIFF(@d,visitancpregnancy.lmp)/30,0) as 'อายุครรภ์(เดือน)'
FROM
person
INNER JOIN ctitle ON person.prename = ctitle.titlecode
INNER JOIN house ON person.hcode = house.hcode
inner JOIN village on house.villcode=village.villcode
INNER JOIN visitancpregnancy ON person.pcucodeperson = visitancpregnancy.pcucodeperson AND person.pid = visitancpregnancy.pid
LEFT JOIN visitancdeliver ON visitancdeliver.pcucodeperson = visitancpregnancy.pcucodeperson AND visitancdeliver.pid = visitancpregnancy.pid AND visitancdeliver.pregno = visitancpregnancy.pregno
where visitancdeliver.pcucode is null 
and round(DATEDIFF(@d,visitancpregnancy.lmp)/30,0)>4