SELECT
pcucode,
off_name,
COUNT(*) AS เป้าหมาย,
SUM(CASE WHEN t.bcg="1" THEN 1 ELSE 0 END) AS BCG,
SUM(CASE WHEN t.hbv1="1" THEN 1 ELSE 0 END) AS HBV1,
SUM(CASE WHEN t.ipv="1" THEN 1 ELSE 0 END) AS IPV,
SUM(CASE WHEN t.opv3="1" THEN 1 ELSE 0 END) AS OPV3,
SUM(CASE WHEN t.dhb3="1" THEN 1 ELSE 0 END) AS DTPHB3,
SUM(CASE WHEN t.mmr="1" THEN 1 ELSE 0 END) AS MMR,
SUM(CASE WHEN t.bcg="1"and t.hbv1="1" AND t.ipv="1" AND t.opv3="1" and t.dhb3="1" AND t.mmr="1" THEN 1 ELSE 0 END) AS EPI_COVER,
SUM(CASE WHEN t.bcg="1"and t.hbv1="1" AND t.ipv="1" AND t.opv3="1" and t.dhb3="1" AND t.mmr="1" THEN 1 ELSE 0 END) / COUNT(*)*100 as percen
FROM tmp_prm_vacc_12561 AS t
INNER JOIN co_office on off_id = pcucode
GROUP BY pcucode
union
select "ภาพรวมทั้งคัพ" as pcucode,"" as off_name,
COUNT(*) AS เป้าหมาย,
SUM(CASE WHEN t.bcg="1" THEN 1 ELSE 0 END) AS BCG,
SUM(CASE WHEN t.hbv1="1" THEN 1 ELSE 0 END) AS HBV1,
SUM(CASE WHEN t.ipv="1" THEN 1 ELSE 0 END) AS IPV,
SUM(CASE WHEN t.opv3="1" THEN 1 ELSE 0 END) AS OPV3,
SUM(CASE WHEN t.dhb3="1" THEN 1 ELSE 0 END) AS DTPHB3,
SUM(CASE WHEN t.mmr="1" THEN 1 ELSE 0 END) AS MMR,
SUM(CASE WHEN t.bcg="1"and t.hbv1="1" AND t.ipv="1" AND t.opv3="1" and t.dhb3="1" AND t.mmr="1" THEN 1 ELSE 0 END) AS EPI_COVER,
SUM(CASE WHEN t.bcg="1"and t.hbv1="1" AND t.ipv="1" AND t.opv3="1" and t.dhb3="1" AND t.mmr="1" THEN 1 ELSE 0 END) / COUNT(*)*100 as percen
FROM tmp_prm_vacc_12561 AS t
INNER JOIN co_office on off_id = pcucode