ของผมเป็นแบบนี้ครับ แต่ก็ยังไม่แน่ใจ ตรง having ว่ามันจะทำให้ยาในบัญชีหายหมดหรือเปล่า
**********************************************************************
select d1.patient_name,v1.hn,DATE_FORMAT(v1.vstdate,'%d/%m/ %Y') as vstdate,d1.rx_queue,d1.patient_pttype_name,k.department,concat(d1.qty_name,' ',dr.units) as qty_name,
right(d1.doctor_order_print_date_time,8) as doctor_rx_time,d1.icode,d1.drug_name,
if(left(d1.ward_name,5)='ยานอก','NED','') as status1,
if(left(d1.ward_name,4)='ยาใน',(if((dr.icode in("1300028","1300160","1300140","1300145","1300297",
"1300214","1300310","1300190","1300363","1300289","1300362","1300380",
"1530002","1530003","1300319","1300127","1530005","1300379","1550012"))
and(d1.patient_pttype_name in(select name from pttype where pttype in("02","03","04","05","07","09","10","11","15"))),'ED**','ED')),'') as status2,
if(dr.icode in(1300193,1300194,1300195,1300014,1300015,1300377,1300281,1300218,1300024
,1300026,1300027,1300028,1300034,1300365,1300038,1300267,1300333,1300049,1300050,1300051,1300052,1300081,1300058
,1300061,1300060,1300042,1300041,1300040,1300043,1300072,1300073,1300074,1300075,1300206,1300359,1300266,1300087
,1300088,1300356,1300095,1300331,1300097,1300257,1300222,1300103,1530008,1300104,1300106,1300105,1300107,1300108
,1300113,1300114,1300118,1300205,1300337,1540029,1300120,1300124,1300125,1300127,1300126,1300129,1300130,1300133
,1300280,1300134,1300135,1300209,1300141,1300146,1300147,1300369,1300156,1300157,1300158,1300164,1300223,1300169
,1300170,1300171,1300173,1300336,1300325,1520014,1300180,1300181,1300182,1300183,1300301,1300241),'@','') as status3,
opn.presc_reason,opn.icode
from doctor_order_print d1
left outer join vn_stat v1 on v1.vn=d1.vn
left outer join ovst o1 on o1.vn=d1.vn
left outer join drugitems dr on dr.icode=d1.icode
left outer join kskdepartment k on k.depcode=o1.main_dep
left outer join ovst_presc_ned opn on opn.vn=d1.vn
where d1.vn="551031141115"
and d1.print_mode=1 and d1.icode not like "3%"
group by d1.icode,opn.icode
having d1.icode=opn.icode