BMS-HOSxP Community
HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: apatnon86 ที่ เมษายน 02, 2014, 14:32:36 PM
-
คือผมต้องการข้อมูลครั้งล่าสุด record นั้นทั้ง record ของคนไข้ที่มี pdx=I10 เพราะบางคนเขามามากว่า 1 ครั้ง ในช่วงเวลาที่กำหนด แต่ต้องการครั้งหลังสุด ยกตัวอย่างตามรูป ต้องการข้อมูลที่ทำสีเขียวใว้
ต้องเขียนคำสั่งอย่างไรครับ ขอบคุณครับ
select *
from vn_stat
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
order by hn
-
ลองดูนะ...
select min(vstdate),a.*
from vn_stat a
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
group by hn
order by hn
-
ทำไมต้องเป็น min ไม่ใช้ max เหรอ
-
ทำไมต้องเป็น min ไม่ใช้ max เหรอ
ใช่ครับ จะรีบกลับบ้าน 5555 ;D
ขอโทษด้วยครับ
-
ลองดูนะ...
select min(vstdate),a.*
from vn_stat a
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
group by hn
order by hn
ไม่ตรงตามความต้องการครับ จากคำสั่งด้านบน (เปลี่ยน min เป็น max) มันจะดึงมาเฉพาะ วันที่รับริการครั้ง ล่าสุดมา แต่ a.* จะดึง visit แรกของคนนั้นมาครับ อย่างเช่นผู้ป่วยมารับบริการทุกวัน วันที่ 1-5 จากคำสั่งด้านบน จะดึง visit ของวันที่ 1 มาแสดง แต่ผมต้องการ visit ครั้งล่าสุด คือ วันที่ 5 ครับ
-
select v.*
from (select max(vn) vn
from vn_stat
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
group by hn
order by hn) a
left join vn_stat v on a.vn=v.vn
-
select v.*
from (select max(vn) vn
from vn_stat
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
group by hn
order by hn) a
left join vn_stat v on a.vn=v.vn
Error ครับ คำสั่งด้านบน
-
select v.*
from (select max(vn) vn
from vn_stat
where vstdate between "2014-01-01" and "2014-03-31"
and pdx="I10"
group by hn
order by hn) as a
left join vn_stat v on a.vn=v.vn
*****
เป้นที่ version ของ mysql
ผมติด..ไม่ค่อยใส่ as