select case when MONTH(q1.c) = '1' then 'ม.ค.-58'
when MONTH(q1.c) = '2' then 'ก.พ.-58'
when MONTH(q1.c) = '3' then 'มี.ค.-58'
when MONTH(q1.c) = '4' then 'เม.ย.-58'
when MONTH(q1.c) = '5' then 'พ.ค.-58'
when MONTH(q1.c) = '6' then 'มิ.ย.-58'
when MONTH(q1.c) = '7' then 'ก.ค.-58'
when MONTH(q1.c) = '8' then 'ส.ค.-58'
when MONTH(q1.c) = '9' then 'ก.ย.-58'
when MONTH(q1.c) = '10' then 'ต.ค.-57'
when MONTH(q1.c) = '11' then 'พ.ย.-57'
ELSE 'ธ.ค.-57' end AS เดือน
,COUNT(distinct q1.a)-1 as 'OP_TM(ครั้ง)',count(distinct q1.b)-1 as 'OP_Visit ทั้งหมด(ครั้ง)'
,round(((COUNT(distinct q1.a)-1)*100)/(count(distinct q1.b)-1),2) as 'ร้อยละ'
from (
select visit.visitno as a,'' as b,visit.visitdate as c
FROM visit
INNER JOIN visitdrug ON visit.pcucode = visitdrug.pcucode AND visit.visitno = visitdrug.visitno
INNER JOIN cdrug ON visitdrug.drugcode = cdrug.drugcode
where(cdrug.drugtype =02 and cdrug.drugtypesub =4 or cdrug.drugtype =10)
GROUP BY visit.visitno
UNION
SELECT '' as a,visit.visitno as b,visit.visitdate as c
FROM visit
INNER JOIN visitdiag ON visit.pcucode = visitdiag.pcucode AND visit.visitno = visitdiag.visitno
where (visitdiag.diagcode not like 'Z%' or visitdiag.diagcode like 'Z48%')
GROUP BY visit.visitno)q1
where q1.c BETWEEN '2014-10-01' and '2015-09-30'
GROUP BY เดือน
ORDER BY q1.c;