SELECT 'จำนวนตรวจสภาวะปริทันต์' AS รายการ
, sum(CASE WHEN substring(visit.visitdate,6,2)='01' THEN 1 ELSE 0 END) AS 'มค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='02' THEN 1 ELSE 0 END) AS 'กพ'
,sum(CASE WHEN substring(visit.visitdate,6,2)='03' THEN 1 ELSE 0 END) AS 'มีค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='04' THEN 1 ELSE 0 END) AS 'เมษ'
,sum(CASE WHEN substring(visit.visitdate,6,2)='05' THEN 1 ELSE 0 END) AS 'พค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='06' THEN 1 ELSE 0 END) AS 'มิย'
,sum(CASE WHEN substring(visit.visitdate,6,2)='07' THEN 1 ELSE 0 END) AS 'กค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='08' THEN 1 ELSE 0 END) AS 'สค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='09' THEN 1 ELSE 0 END) AS 'กย'
,sum(CASE WHEN substring(visit.visitdate,6,2)='10' THEN 1 ELSE 0 END) AS 'ตค'
,sum(CASE WHEN substring(visit.visitdate,6,2)='11' THEN 1 ELSE 0 END) AS 'พย'
,sum(CASE WHEN substring(visit.visitdate,6,2)='12' THEN 1 ELSE 0 END) AS 'ธค'
,count(person.pid) AS รวม
FROM visit INNER JOIN visitdentalcheck ON visit.pcucode=visitdentalcheck.pcucode AND visit.visitno=visitdentalcheck.visitno
INNER JOIN person ON person.pcucodeperson=visit.pcucodeperson AND person.pid=visit.pid
WHERE visit.visitdate BETWEEN '2014-10-01' AND '2015-09-30'
#AND YEAR(FROM_DAYS(DATEDIFF(visit.visitdate ,person.birth ) ) ) >= 60
AND getAgeYearNum(person.birth,visit.visitdate) >= 60
#GROUP BY date_format(visit.visitdate,'%Y-%m') 