SELECT
co_office.distid AS `รหัสอำเภอ`,
co_office.cup_code AS `รหัสcup`,
co_office.off_id AS `รหัสสถานบริการ`,
co_office.off_name AS `ชื่อสถานบริการ`,
Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid) AS `จำนวนผู้ป่วยเบาหวาน`
FROM
chronic
LEFT OUTER JOIN co_office ON chronic.pcucode = co_office.off_id
INNER JOIN labfu ON chronic.pcucode = labfu.pcucode AND chronic.pid = labfu.pid
WHERE
chronic.chronic BETWEEN 'E10' AND 'E149'
GROUP BY
co_office.off_id
ORDER BY
`รหัสอำเภอ` ASC,
`รหัสcup` ASC,
`รหัสสถานบริการ` ASC
select case when count(*) >0 then count(*) else 0 end result from table_name
ประยุกต์ใช้แบบด้านล่าง Code (SQL)
SELECT
co_office.distid AS `รหัสอำเภอ`,
co_office.cup_code AS `รหัสcup`,
co_office.off_id AS `รหัสสถานบริการ`,
co_office.off_name AS `ชื่อสถานบริการ`,
case when
Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid)>0
then Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid)
else 0 end `จำนวนผู้ป่วยเบาหวาน`
FROM
chronic
LEFT OUTER JOIN co_office ON chronic.pcucode = co_office.off_id
INNER JOIN labfu ON chronic.pcucode = labfu.pcucode AND chronic.pid = labfu.pid
WHERE
chronic.chronic BETWEEN 'E10' AND 'E149'
GROUP BY
co_office.off_id
ORDER BY
`รหัสอำเภอ` ASC,
`รหัสcup` ASC,
`รหัสสถานบริการ` ASC
SELECT
co_office.distid AS `รหัสอำเภอ`,
co_office.cup_code AS `รหัสcup`,
co_office.off_id AS `รหัสสถานบริการ`,
co_office.off_name AS `ชื่อสถานบริการ`,
case WHEN
Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid)>0
THEN Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid)
ELSE 0 END `เบาหวานที่มารับบริการในช่วง1ตค55-28กพ56และhba1cนก7%`
FROM
chronic
LEFT OUTER JOIN co_office ON chronic.pcucode = co_office.off_id
INNER JOIN labfu ON chronic.pcucode = labfu.pcucode AND chronic.pid = labfu.pid
WHERE
chronic.chronic BETWEEN 'E10' AND 'E149' AND
labfu.date_serv BETWEEN 20121001 AND 20130228 AND
labfu.labtest = 05 AND
labfu.labresult < 7
GROUP BY
co_office.off_id
ORDER BY
`รหัสอำเภอ` ASC,
`รหัสcup` ASC,
`รหัสสถานบริการ` ASC
SELECT
distid AS DistID,
cup_code AS Cup,
off_id AS Hoscode,
off_name AS Hosname,
(SELECT
Count(DISTINCT chronic.cid,chronic.pcucode,chronic.pid) AS `จำนวนผู้ป่วยเบาหวาน`
FROM
chronic
#LEFT OUTER JOIN co_office ON chronic.pcucode = co_office.off_id
INNER JOIN labfu ON chronic.pcucode = labfu.pcucode AND chronic.pid = labfu.pid
WHERE
chronic.chronic BETWEEN 'E10' AND 'E149'
GROUP BY
o.off_id)
FROM
co_office o
WHERE LENGTH(cup_code) = 5
GROUP BY
off_id