หาจำนวนผู้ป่วยหอบหืด Re admid ภายใน 28 วัน กี่คนราย พอจะมีวิธีหาอย่างไรบ้างคับ อยากได้ sql คับ
ตามนี้ครับ
select a1.an,a1.hn,a1.pdx as pdx1,
s1.name as clinic1,a2.pdx as pdx2,s2.name as clinic2,a1.regdate,a2.regdate,a1.dchdate,a2.dchdate,a2.lastvisit,a1.age_y,count(a1.pdx) as ptco
from an_stat a1
left outer join an_stat a2 on a1.hn=a2.hn
left outer join patient p on p.hn=a1.hn
left outer join spclty s1 on s1.spclty=a1.spclty
left outer join spclty s2 on s2.spclty=a2.spclty
where a2.regdate between a1.regdate and (a1.dchdate+28) and a1.pdx = a2.pdx
and a1.vn<>a2.vn
and a1.dchdate between '2010-02-01' and '2012-09-30'
group by a1.pdx
order by ptco desc
limit 20