ทำได้แล้วครับ
select v.vn,v.hn,v.vstdate,ov.vsttime,IF(v.lastvisit_hour>24,FLOOR(v.lastvisit_hour/24),CEILING(v.lastvisit_hour/24))as lastvisit_day,v.lastvisit_hour,
IF(v.lastvisit_hour>24,
DATE_FORMAT(MAKEDATE(2009,(DAYOFYEAR(DATE_FORMAT(concat(ov.vstdate," ",ov.vsttime),GET_FORMAT(DATETIME,"USA")))-(FLOOR(v.lastvisit_hour/24)))),"%Y-%m-%d"),
DATE_FORMAT(MAKEDATE(2009,(DAYOFYEAR(DATE_FORMAT(concat(ov.vstdate," ",ov.vsttime),GET_FORMAT(DATETIME,"USA")))-(CEILING(v.lastvisit_hour/24)))),"%Y-%m-%d"))as lastvisit_date,
o.temperature,o.cc,op.icode
from vn_stat v
left join opdscreen o on o.vn=v.vn
left join ovst ov on ov.vn=v.vn
left join opitemrece op on op.vn=v.vn
where (v.vstdate between '2009-08-01' and '2009-08-31') and v.lastvisit_hour<48 and o.temperature>=38
and (o.cc like "%ไข้%" or o.cc like "%ไอ%" or o.cc like "%เจ็บคอ%") and op.icode=1460039
and v.hn=(select v2.hn from vn_stat v2 left join opdscreen o2 on o2.vn=v2.vn
where v2.hn=v.hn and
v2.vstdate=(
IF(v.lastvisit_hour>24,
DATE_FORMAT(MAKEDATE(2009,(DAYOFYEAR(DATE_FORMAT(concat(ov.vstdate," ",ov.vsttime),GET_FORMAT(DATETIME,"USA")))-(FLOOR(v.lastvisit_hour/24)))),"%Y-%m-%d"),
DATE_FORMAT(MAKEDATE(2009,(DAYOFYEAR(DATE_FORMAT(concat(ov.vstdate," ",ov.vsttime),GET_FORMAT(DATETIME,"USA")))-(CEILING(v.lastvisit_hour/24)))),"%Y-%m-%d"))
)
and o2.temperature>=38 and (o2.cc like "%ไข้%" or o2.cc like "%ไอ%" or o2.cc like "%เจ็บคอ%")
)
กว่าจะมั่วออกมาได้ ตัวแปรที่เปลี่ยนแปลงได้ คือ icode ของยา Oseltamivir , lastvisit_hour<48, CC , temperature ครับ
ขอบคุณมากครับ