 SELECT chospital.hoscode
,chospital.hosname
,COUNT(DISTINCT CASE WHEN women.fptype = '1' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดชั่วคราวยาเม็ด'
,COUNT(DISTINCT CASE WHEN women.fptype = '2' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดชั่วคราวยาฉีด'
,COUNT(DISTINCT CASE WHEN women.fptype = '5' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดชั่วคราวถุงยาง'
,COUNT(DISTINCT CASE WHEN women.fptype = '4' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดกึ่งถาวรห่วง'
,COUNT(DISTINCT CASE WHEN women.fptype = '3' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดกึ่งถาวรยาฝัง'
,COUNT(DISTINCT CASE WHEN women.fptype = '7' THEN person.pid ELSE NULL END) AS 'คุมกำเนิดถาวรทำหมัน'

FROM person
INNER JOIN visit on person.pid = visit.pid AND person.pcucodeperson = visit.pcucode
LEFT JOIN women ON visit.pcucodeperson = women.pcucodeperson AND visit.pid = women.pid
LEFT JOIN persondeath ON person.pid = persondeath.pid AND person.pcucodeperson = persondeath.pcucodeperson
LEFT JOIN chospital ON person.pcucodeperson = chospital.hoscode
INNER JOIN house ON house.pcucode = person.pcucodeperson AND house.hcode = person.hcode
WHERE persondeath.pid IS NULL
AND person.typelive IN ('0','1','3')
AND RIGHT(house.villcode,2)!= '00'
AND YEAR( FROM_DAYS( DATEDIFF( NOW( ) ,person.birth ) ) ) BETWEEN 15 AND 19
AND visit.visitdate BETWEEN '2013-10-01' AND '2014-04-30'
GROUP BY chospital.hoscode;