BMS-HOSxP Community

HOSxP => MySQL => ข้อความที่เริ่มโดย: mr.yung ที่ พฤษภาคม 31, 2012, 12:08:50 PM

หัวข้อ: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: mr.yung ที่ พฤษภาคม 31, 2012, 12:08:50 PM
มีคำถามเกี่ยวกับการใช้คำสั่ง mysql ครับ

คือว่าจากคำสั่ง sql ด้านล่างนี้  ผมต้องการหา hn ที่มีการให้ diag โรคคือ Z392 และ Z014  ซึ่งมันก็ได้ผลและแสดงออกมา  แต่ทีนี้ถ้าผมต้องการจะให้แสดงจำนวนของ hn ที่หาได้หละครับ  ผมหมายถึงการ count hn ที่หาได้อีกทีนึง  เราต้องปรับการเขียน sql อย่างไร  ดังภาพด้านล่าง  ซึ่งได้มา 5 เรคคอร์ด

select hn, count(hn) as cou from ovstdiag
where icd10 in("Z392", "Z014")
and vstdate between "2012-04-01" and "2012-04-30"
group by hn
having cou>1
หัวข้อ: Re: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: pop_hosxp ที่ พฤษภาคม 31, 2012, 12:40:44 PM
ใช้ count(distinct hn) ครับ
หัวข้อ: Re: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: surachat1234 ที่ พฤษภาคม 31, 2012, 12:41:43 PM
ใช่นับ hn ที่หาได้หรือมัย

SELECT count(A.hn)
 from (select hn, count(hn) as cou from ovstdiag
where icd10 in("Z392", "Z014")
and vstdate between "2012-04-01" and "2012-04-30"
group by hn
having cou>1) A
หัวข้อ: Re: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: เกื้อกูล ครับ.. ที่ พฤษภาคม 31, 2012, 12:52:03 PM
มีคำถามเกี่ยวกับการใช้คำสั่ง mysql ครับ

คือว่าจากคำสั่ง sql ด้านล่างนี้  ผมต้องการหา hn ที่มีการให้ diag โรคคือ Z392 และ Z014  ซึ่งมันก็ได้ผลและแสดงออกมา  แต่ทีนี้ถ้าผมต้องการจะให้แสดงจำนวนของ hn ที่หาได้หละครับ  ผมหมายถึงการ count hn ที่หาได้อีกทีนึง  เราต้องปรับการเขียน sql อย่างไร  ดังภาพด้านล่าง  ซึ่งได้มา 5 เรคคอร์ด

select hn, count(hn) as cou,count(distinct hn) as aou from ovstdiag
where icd10 in("Z392", "Z014")
and vstdate between "2012-04-01" and "2012-04-30"
group by hn
having cou>1

หัวข้อ: Re: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: Dear ที่ พฤษภาคม 31, 2012, 13:13:52 PM
มีคำถามเกี่ยวกับการใช้คำสั่ง mysql ครับ

คือว่าจากคำสั่ง sql ด้านล่างนี้  ผมต้องการหา hn ที่มีการให้ diag โรคคือ Z392 และ Z014  ซึ่งมันก็ได้ผลและแสดงออกมา  แต่ทีนี้ถ้าผมต้องการจะให้แสดงจำนวนของ hn ที่หาได้หละครับ  ผมหมายถึงการ count hn ที่หาได้อีกทีนึง  เราต้องปรับการเขียน sql อย่างไร  ดังภาพด้านล่าง  ซึ่งได้มา 5 เรคคอร์ด

select hn, count(hn) as cou from ovstdiag
where icd10 in("Z392", "Z014")
and vstdate between "2012-04-01" and "2012-04-30"
group by hn
having cou>1

ลองแบบนี้ดูค่ะ  ;) ;)
select count(distinct(o.hn)) as amount_hn
from ovstdiag o,(select hn, count(hn) as cou from ovstdiag
                   where icd10 in("Z392", "Z014") and vstdate between "2012-04-01" and "2012-04-30"
                   group by hn having cou>1 ) as temp
where o.hn=temp.hn
หัวข้อ: Re: สอบถามการเขียน sql ครับ
เริ่มหัวข้อโดย: mr.yung ที่ พฤษภาคม 31, 2012, 13:15:16 PM
ใช่นับ hn ที่หาได้หรือมัย

SELECT count(A.hn)
 from (select hn, count(hn) as cou from ovstdiag
where icd10 in("Z392", "Z014")
and vstdate between "2012-04-01" and "2012-04-30"
group by hn
having cou>1) A


ขอบคุณมากครับ  นี่แหละ ใช่เลย