รง.504
select a.*,ifnull(d.amount,0) as amount
from rpt_504_name a
left join (select b.id,count(b.id) as amount from rpt_504_code b,ovstdiag c
where c.icd10 between b.code1 and b.code2
and c.vstdate between '2009-1-1' and '2009-1-31'
group by b.id) d on d.id=a.id
order by a.id
รง.505
select a.*,ifnull(d.amount,0) as amount
from rpt_505_name a
left join (select x.id,count(x.id) as amount from rpt_505_code x,ipt y,iptdiag z where z.icd10 between x.code1 and x.code2
and y.an=z.an and y.dchdate between '2009-1-1' and '2009-1-31'
group by x.id) d on d.id=a.id
order by a.id