select a.hn,a.an,b.an,concat(p.pname,p.fname," ",p.lname)as ptname,b.pdx as old_pdx,a.pdx,a.dx0,a.dx1,a.dx2,a.dx3,a.dx4,a.dx5,
a.regdate,a.dchdate,b.regdate,b.dchdate,a.age_y,a.age_m,a.age_d,a.aid,w.name,i.name as pdxname,a.lastvisit
from an_stat a
left outer join an_stat b on a.hn=b.hn and a.pdx=b.pdx and a.an>b.an
left outer join patient p on a.hn=p.hn
left outer join icd101 i on i.code=a.pdx
left outer join ipt ip on ip.an=a.an
left outer join ward w on w.ward=a.ward
where (a.dchdate between "2011-06-15" and "2011-06-15" ) and a.lastvisit <= "28"
and ((to_days(a.regdate))-(to_days(b.dchdate)))<="28"
and a.pdx is not null and a.pdx<>"" and a.pdx not like "Z%"
order by a.an
ลองดูครับพี่นก