BMS-HOSxP Community

HOSxP => การเขียน SQL Script => ข้อความที่เริ่มโดย: Neo ที่ เมษายน 19, 2009, 21:33:49 PM

หัวข้อ: เรียนถาม อาจารย์ทุกท่าน เรื่อง คำสั่ง Sql
เริ่มหัวข้อโดย: Neo ที่ เมษายน 19, 2009, 21:33:49 PM
มีโจทย์อยู่ว่า อยากได้คนไข้ที่มาด้วยโรคหัวใจ (I300 - I529) แต่คนไข้ต้องมีใช่คนไข้ในคลินิกพิเศษ

คำสั่งที่ผมเขียนครับ เขียนต่อให้หน่อย
select p.clinic,vn.hn,concat(vn.pdx,'  ',i.name) as pdxname,
concat(p.pname,p.fname,'  ',p.lname) as fullname,p.informaddr,vn.age_y from vn_stat vn
inner join patient p on p.hn=vn.hn
inner join icd101 i on i.code=vn.pdx
where vn.pdx between 'I300' and 'I529' and vn.vstdate between '2008-10-01' and '2009-03-31'
group by vn.hn
order by vn.hn asc
หัวข้อ: Re: เรียนถาม อาจารย์ทุกท่าน เรื่อง คำสั่ง Sql
เริ่มหัวข้อโดย: perjer4675 ที่ เมษายน 20, 2009, 08:15:01 AM
select p.clinic,vn.hn,concat(vn.pdx,'  ',i.name) as pdxname,
concat(p.pname,p.fname,'  ',p.lname) as fullname,p.informaddr,vn.age_y
from vn_stat vn
inner join patient p on p.hn=vn.hn
inner join icd101 i on i.code=vn.pdx
where vn.pdx between 'I300' and 'I529' and vn.vstdate between '2008-03-01' and '2009-03-01'
and vn.hn in (select hn from clinicmember)
group by vn.hn
order by vn.hn asc

หัวข้อ: Re: เรียนถาม อาจารย์ทุกท่าน เรื่อง คำสั่ง Sql
เริ่มหัวข้อโดย: perjer4675 ที่ เมษายน 20, 2009, 08:22:07 AM
มีโจทย์อยู่ว่า อยากได้คนไข้ที่มาด้วยโรคหัวใจ (I300 - I529) แต่คนไข้ต้องมีใช่คนไข้ในคลินิกพิเศษ

ตกลงมิใช่ หรือ ว่ามีใช้ ครับ ก็แก้ตามนี้เองนะงับ

ถ้ามีใช้ ก็ใช้ in

select p.clinic,vn.hn,concat(vn.pdx,'  ',i.name) as pdxname,
concat(p.pname,p.fname,'  ',p.lname) as fullname,p.informaddr,vn.age_y
from vn_stat vn
inner join patient p on p.hn=vn.hn
inner join icd101 i on i.code=vn.pdx
where vn.pdx between 'I300' and 'I529' and vn.vstdate between '2008-03-01' and '2009-03-01'
and vn.hn in (select hn from clinicmember)
group by vn.hn
order by vn.hn asc

ถ้ามิใช่ ก็ใช้ not in

select p.clinic,vn.hn,concat(vn.pdx,'  ',i.name) as pdxname,
concat(p.pname,p.fname,'  ',p.lname) as fullname,p.informaddr,vn.age_y
from vn_stat vn
inner join patient p on p.hn=vn.hn
inner join icd101 i on i.code=vn.pdx
where vn.pdx between 'I300' and 'I529' and vn.vstdate between '2008-03-01' and '2009-03-01'
and vn.hn not in (select hn from clinicmember)
group by vn.hn
order by vn.hn asc
หัวข้อ: Re: เรียนถาม อาจารย์ทุกท่าน เรื่อง คำสั่ง Sql
เริ่มหัวข้อโดย: Neo ที่ เมษายน 20, 2009, 08:32:13 AM
ขอบคุณครับ ทดสอบ Code ของท่านแล้วใช้ได้เลครับ
1. เพิ่ม and vn.hn in (select hn from clinicmember) จะเอาคนไข้ในคลินิกพิเศษที่มาด้วยโรคหัวใจ เท่านั้น ซึ่งตามโจทย์ ไม่ถูก
2. เพิ่ม and vn.hn not in (select hn from clinicmember) จะเอาคนไข้ไม่เป็นคนไข้ในคลินิกพิเศษที่มาด้วยโรคหัวใจ ถูกต้องครับ


ขอบคุณมากครับ ;D ;D ;D