เนื่องจากวันเวลาต่างกันในกรณี Lab ส่งนอกทำให้ลบกันแล้วมีปัญหา เท่าที่ผมเขียนไว้ได้แค่นี้รบกวนผู้รู้ต่อยอดให้ทีครับขอเงื่อนไขหน่อยครับ
select p.hn,se.vstdate,concat(p.pname,p.fname," ",p.lname) as ptname,ov.vsttime,sc.rx_time,se.service3,se.service4,
se.service11,se.service5,se.service12,se.service16,v.pdx,
sec_to_time(time_to_sec(se.service4)-time_to_sec(ov.vsttime)) AS opd_wait_card,
sec_to_time(time_to_sec(se.service5)-time_to_sec(se.service11)) AS doctor_wait_time,
sec_to_time(time_to_sec(se.service4)-time_to_sec(se.service3)) AS opd_wait_time,
sec_to_time(time_to_sec(se.service16)-time_to_sec(se.service12)) AS pham_wait_time,
sec_to_time(time_to_sec(se.service11)-time_to_sec(se.service4)) AS opd_time,
sec_to_time(time_to_sec(se.service12)-time_to_sec(se.service5)) AS dr_time,
sec_to_time(time_to_sec(se.service16)-time_to_sec(se.service3)) AS all_time,
sec_to_time(time_to_sec(se.service16)-time_to_sec(se.service4)) AS all_opd_wait_time
from patient p
left outer join ovst ov on ov.hn=p.hn
left outer join screen_doctor sc on sc.vn=ov.vn
left outer join service_time se on se.vn=ov.vn
left outer join vn_stat v on v.vn=ov.vn
left outer join doctor do on do.code=v.dx_doctor
where sc.depcode = 004 and se.vstdate between'2012-07-01'and'2012-07-30' and not (p.firstday=se.vstdate) and se.service4 IS NOT NULL and se.service5 IS NOT NULL and se.service11 IS NOT NULL
and se.service16 IS NOT NULL
order by se.vstdate