ถ้าแยกระหว่าง OPD กับ IPD ครับ
ลองอันนี้ครับ
select v.pttype, p.name, o.department, count(distinct o.vn) count, count(o.vn)
from opitemrece_summary o
left outer join vn_stat v on v.vn=o.vn
left outer join pttype p on p.pttype=v.pttype
where o.department='OPD' and o.icode like '1%'
and o.rxdate between '2008-01-01' and '2008-01-01'
group by v.pttype
union
select a.pttype, p.name, o.department, count(distinct a.an) count, count(o.vn)
from opitemrece_summary o
left outer join an_stat a on a.an=o.vn
left outer join pttype p on p.pttype=a.pttype
where o.department='ipd' and o.icode like '1%'
and o.rxdate between '2008-01-01' and '2008-01-01'
group by a.pttype