4
« เมื่อ: เมษายน 23, 2013, 14:06:12 PM »
select v.hospsub,h.name ,count(distinct(d.hn)) as kon,count(distinct(d.vn)) as krung, count(d.hn) as ngan,
count(distinct(case when v.age_y <= 2 then d.vn end) ) as age_0_2,
count(distinct(case when v.age_y >= 3 and v.age_y <= 5 then d.vn end) ) as age_3_5,
count(distinct(case when v.age_y >= 6 and v.age_y <= 14 then d.vn end) ) as age6_14,
count(distinct(case when v.age_y >= 15 and v.age_y <= 40 then d.vn end) ) as age15_40,
count(distinct(case when v.age_y >= 41 and v.age_y <= 59 then d.vn end) ) as age41_59,
count(distinct(case when v.age_y >= 60 then d.vn end) ) as age_60more
from dtmain d
left outer join vn_stat v on v.vn = d.vn
left outer join hospcode h on h.hospcode=v.hospsub
where d.vstdate between '2013-01-01' and '2013-01-31' and v.hospmain=10874 and hospsub<>""
group by v.hospsub