SELECT ov.vn,ov.hn,ov.an,ov.regdate,vs.dchdate,vs.sex,vs.age_y as age,ov.pttype,
pp.`name` as ptname,pc.`name` as rights,
op.income as income_code,ic.`name` as income_name,op.icode,sd.`name` as drugname,op.qty,op.unitprice,
op.sum_price,sd.cost,NULL as dep_code,vs.dx_doctor as doctor,vs.admdate_cut24 as los,vs.drg,vs.rw,ov.adjrw,vs.ward,
vs.pdx,id.`name` as pdxname,
vs.income,vs.inc01,vs.inc02,vs.inc03,vs.inc04,vs.inc05,vs.inc06,vs.inc07,vs.inc08,vs.inc09,vs.inc10,
vs.inc11,vs.inc12,vs.inc13,vs.inc14,vs.inc15,vs.inc16,vs.inc17,vs.inc03 + vs.inc04 + vs.inc17 as IncDrug,
SEC_TO_TIME(TIMESTAMPDIFF(SECOND,CONCAT(ov.regdate,' ',ov.regtime),CONCAT(ov.dchdate,' ',ov.dchtime))) as Duration
FROM ipt as ov
LEFT OUTER JOIN an_stat as vs ON(ov.an=vs.an)
LEFT OUTER JOIN pttype as pp ON(ov.pttype=pp.pttype)
LEFT OUTER JOIN pcode as pc ON(pp.pcode=pc.`code`)
LEFT OUTER JOIN opitemrece as op ON(ov.an=op.an)
LEFT OUTER JOIN income as ic ON(op.income=ic.income)
LEFT OUTER JOIN s_drugitems as sd ON(op.icode=sd.icode)
LEFT OUTER JOIN icd101 as id ON(vs.pdx=id.`code`)
WHERE
(vs.regdate BETWEEN '2013-10-01' AND '2014-9-30') AND vs.dchdate >= '2013-10-01'
LIMIT 200