IPD
select "XXXXX" as hcode,a.an,a.hn,a.vn,p.pname,p.fname,p.lname,p.birthday,p.sex,a.regdate,a.dchdate,w.name as wardname,
pty.name as pttypename,a.pdx,a.dx0,a.dx1,a.dx2,a.dx3,a.dx4,a.dx5,a.op0,a.op1,a.op2,a.op3,a.op4,a.op5,a.op6,
sum(if(in2.group2='01',c.rcptamt,0)) as inc01,
sum(if(in2.group2='02',c.rcptamt,0)) as inc02,
sum(if(in2.group2='03',c.rcptamt,0)) as inc03,
sum(if(in2.group2='04',c.rcptamt,0)) as inc04,
sum(if(in2.group2='05',c.rcptamt,0)) as inc05,
sum(if(in2.group2='06',c.rcptamt,0)) as inc06,
sum(if(in2.group2='07',c.rcptamt,0)) as inc07,
sum(if(in2.group2='08',c.rcptamt,0)) as inc08,
sum(if(in2.group2='09',c.rcptamt,0)) as inc09,
sum(if(in2.group2='10',c.rcptamt,0)) as inc10,
sum(if(in2.group2='11',c.rcptamt,0)) as inc11,
sum(if(in2.group2 in('12','17'),c.rcptamt,0)) as inc12,
sum(if(in2.group2='13',c.rcptamt,0)) as inc13,
sum(if(in2.group2='14',c.rcptamt,0)) as inc14,
sum(if(in2.group2='15',c.rcptamt,0)) as inc15,
sum(if(in2.group2='16',c.rcptamt,0)) as inc16,
sum(if(in2.income='72',c.rcptamt,0)) as inc17,
a.los,a.drg,i.rw,i.adjrw
from an_stat a
left JOIN patient p on p.hn=a.hn
LEFT JOIN pttype pty on pty.pttype=a.pttype
left join incith c on c.an = a.an
left outer join ward w on w.ward = a.ward
left outer join ipt i on i.an = a.an
left join income in2 on in2.income=c.income
WHERE a.dchdate BETWEEN '2011-04-01' and '2011-04-30'
group by a.an
OPD
SELECT "XXXXX" as hcode,a.vn,p.hn,a.cid,p.pname,p.fname,p.lname,p.birthday,
p.sex,a.vstdate,pty.name as pttypename,a.pdx,a.dx0,a.dx1,a.dx2,a.dx3,a.dx4,a.dx5,
a.op0,a.op1,a.op2,a.op3,a.op4,a.op5,
sum(if(in2.group2='01',c.rcptamt,0)) as inc01,
sum(if(in2.group2='02',c.rcptamt,0)) as inc02,
sum(if(in2.group2='03',c.rcptamt,0)) as inc03,
sum(if(in2.group2='04',c.rcptamt,0)) as inc04,
sum(if(in2.group2='05',c.rcptamt,0)) as inc05,
sum(if(in2.group2='06',c.rcptamt,0)) as inc06,
sum(if(in2.group2='07',c.rcptamt,0)) as inc07,
sum(if(in2.group2='08',c.rcptamt,0)) as inc08,
sum(if(in2.group2='09',c.rcptamt,0)) as inc09,
sum(if(in2.group2='10',c.rcptamt,0)) as inc10,
sum(if(in2.group2='11',c.rcptamt,0)) as inc11,
sum(if(in2.group2 in('12','17'),c.rcptamt,0)) as inc12,
sum(if(in2.group2='13',c.rcptamt,0)) as inc13,
sum(if(in2.group2='14',c.rcptamt,0)) as inc14,
sum(if(in2.group2='15',c.rcptamt,0)) as inc15,
sum(if(in2.group2='16',c.rcptamt,0)) as inc16,
sum(if(in2.income='72',c.rcptamt,0)) as inc17
from vn_stat a
left JOIN patient p on p.hn=a.hn
LEFT JOIN pttype pty on pty.pttype=a.pttype
left join incoth c on a.vn = c.vn
left join income in2 on in2.income=c.income
WHERE a.vstdate BETWEEN '2011-10-01' and '2011-10-31'
group by a.vn