SELECT
co_office.distid AS รหัสอำเภอ
,co_office.cup_code AS รหัสCUP
,co_office.off_id AS รหัสสถานบริการ
,co_office.off_name AS ชื่อสถานบริการ
,SUM(CASE WHEN anc.ga BETWEEN '0' AND '11' THEN 1 ELSE 0 END) AS 'ครั้งที่1(0-11)'
,SUM(CASE WHEN anc.ga BETWEEN '12' AND '25' THEN 1 ELSE 0 END) AS 'ครั้งที่2(12-25)'
,SUM(CASE WHEN anc.ga BETWEEN '26' AND '31' THEN 1 ELSE 0 END) AS 'ครั้งที่3(26-31)'
,SUM(CASE WHEN anc.ga BETWEEN '32' AND '37' THEN 1 ELSE 0 END) AS 'ครั้งที่4(32-37)'
,SUM(CASE WHEN anc.ga >'38' THEN 1 ELSE 0 END) AS 'ครั้งที่5(38up)'
,COUNT(DISTINCT anc.pcucode,anc.pid) AS 'ฝากท้องทั้งหมด:คน'
FROM anc
INNER JOIN co_office ON anc.pcucode = co_office.off_id
WHERE
anc.date_serv BETWEEN 20121001 AND 20130331
GROUP BY
anc.pcucode
ORDER BY
co_office.distid ASC,
co_office.cup_code ASC,
anc.pcucode ASC
CREATE TABLE `anc` (
`pcucode` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`pid` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`seq` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`date_serv` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`aplace` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`gravida` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`ancno` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`ga` varchar(3) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`ancres` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`d_update` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`cid` varchar(13) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`flagstatus` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`flagnhsoauto` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`createdate` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`importdate` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`importfilename` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`exportdate` varchar(14) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`exportfilename` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`pcusynclist` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`pcucode`, `pid`, `date_serv`),
INDEX `ancno_idx` (`ancno`, `gravida`, `flagnhsoauto`) USING BTREE ,
INDEX `date_serv_idx` (`date_serv`) USING BTREE ,
INDEX `d_update_idx` (`d_update`) USING BTREE ,
INDEX `cid_idx` (`cid`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=COMPACT
;