ลองเอาไปปรับใช้ดูนะครับ
select v.hn,concat(p.pname,p.fname," ",p.lname) as full_name,v.sex,y.name as pttype,v.vstdate as visit_date,v.pdx as diag,i.name as diag_name,
v.age_y,v.age_m,v.age_d,
case
when length(p.moopart) = 1 then concat("0",p.moopart)
else p.moopart
end as moo,
t.full_name as address
from vn_stat v
left outer join patient p on p.hn=v.hn
left outer join thaiaddress t on p.chwpart=t.chwpart and p.amppart=t.amppart and p.tmbpart=t.tmbpart
left outer join icd101 i on i.code=v.pdx
left outer join pttype y on y.pttype=v.pttype
where v.vstdate between "2014-01-01" and "2014-01-31" and length(v.pdx) <> 0
order by moo,t.full_name