BMS-HOSxP Community

HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: Bluebird ที่ พฤษภาคม 18, 2012, 16:45:32 PM

หัวข้อ: กรองผู้ป่วย DM ที่ยังไม่เคยรับยาบางตัว แบบวันที่มาล่าสุด
เริ่มหัวข้อโดย: Bluebird ที่ พฤษภาคม 18, 2012, 16:45:32 PM
ห้องยาต้อง
ผู้ป่วย DM อายุ 50 ปีขึ้นไป ที่ยังไม่เคยได้รับยา (ระบุ icode 1000038,1530003)
โดยจะเอา
1.HN
2.ชื่อสกุล
3.อายุ
4.ค่า FBS/DTX
5.มาล่าสุด
6.นัดครั้งต่อไป

select pt.hn, concat(pt.pname,pt.fname,"  ",pt.lname) as fullname,pt.cid,pt.birthday,ov.age_y,ov.vstdate,oa.nextdate
from ovstdiag ovst
left outer join patient pt on pt.hn=ovst.hn
left outer join vn_stat ov on ov.vn=ovst.vn
left outer join opitemrece op on op.vn=ov.vn
left outer join oapp oa on oa.vn=ov.vn
where  ov.vstdate between "2011-10-01" and "2012-04-30"
and ov.age_y>=50
and (ovst.icd10 >= "E10" and ovst.icd10 <= "E148")
and op.icode not in ("1530003","1000038")


ส่วนท้ายต้องใช้ Group by hn หรืออะไรครับ
ใช้ Group by hn จะออกมาเป็นวันแรกที่มา ไม่ใช่วันล่าสุดครับ

หัวข้อ: Re: กรองผู้ป่วย DM ที่ยังไม่เคยรับยาบางตัว แบบวันที่มาล่าสุด
เริ่มหัวข้อโดย: golf_win ที่ พฤษภาคม 18, 2012, 19:18:04 PM
select pt.hn, concat(pt.pname,pt.fname,"  ",pt.lname) as fullname,pt.cid,pt.birthday,ov.age_y,max(ov.vstdate) as last_vstdate,max(oa.nextdate) as last_nextdate
from ovstdiag ovst
left outer join patient pt on pt.hn=ovst.hn
left outer join vn_stat ov on ov.vn=ovst.vn
left outer join opitemrece op on op.vn=ov.vn
left outer join oapp oa on oa.vn=ov.vn
where  ov.vstdate between "2011-10-01" and "2012-04-30"
and ov.age_y>=50
and (ovst.icd10 >= "E10" and ovst.icd10 <= "E148")
and op.icode not in ("1530003","1000038")
group by pt.hn