SELECT
CONCAT(c.titlename,p.fname,' ',p.lname) AS `ชื่อ - สกุล`,
getAgeYearNum (p.birth,CURDATE()) AS `อายุ`,
h.hno AS `บ้านเลขที่`,
v.villno AS `หมู่ที่`,
MAX(CASE WHEN v1.visitdate BETWEEN '20141001' AND '20141031' THEN v1.pressure ELSE NULL END) AS 'ต.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20141101' AND '20141130' THEN v1.pressure ELSE NULL END) AS 'พ.ย.',
MAX(CASE WHEN v1.visitdate BETWEEN '20141201' AND '20141231' THEN v1.pressure ELSE NULL END) AS 'ธ.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150101' AND '20150131' THEN v1.pressure ELSE NULL END) AS 'มค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150201' AND '20150231' THEN v1.pressure ELSE NULL END) AS 'ก.พ.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150301' AND '20150331' THEN v1.pressure ELSE NULL END) AS 'มี.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150401' AND '20150430' THEN v1.pressure ELSE NULL END) AS 'เม.ย.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150501' AND '20150531' THEN v1.pressure ELSE NULL END) AS 'พ.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150601' AND '20150630' THEN v1.pressure ELSE NULL END) AS 'มิ.ย.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150701' AND '20150731' THEN v1.pressure ELSE NULL END) AS 'ก.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150801' AND '20150831' THEN v1.pressure ELSE NULL END) AS 'ส.ค.',
MAX(CASE WHEN v1.visitdate BETWEEN '20150901' AND '20150930' THEN v1.pressure ELSE NULL END) 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
INNER JOIN visit AS v1 ON p.pcucodeperson = v1.pcucodeperson AND p.pid = v1.pid
WHERE
 v1.visitdate BETWEEN '20141001' AND '20150930'
AND p.typelive IN (0,1,3)
AND p.dischargetype = 9
AND p.pid NOT IN (SELECT persondeath.pid FROM persondeath)
AND (left(pressure,3) > '160' AND right(pressure,2) < '50')
GROUP BY
p.pid
ORDER BY
v.villno
#create by rambo