BMS-HOSxP Community
HOSxP => แจ้งปัญหา / ขอความช่วยเหลือ => ข้อความที่เริ่มโดย: panus_t ที่ มีนาคม 07, 2013, 10:34:50 AM
-
select an.spclty,sp.name,count(select sex from an_stat where sex="1") as sex1,count(select sex from an_stat where sex="2") as sex2
from ovst ov
left outer join an_stat an on an.vn=ov.vn
left outer join spclty sp on sp.spclty=vn.spclty
where an.regdate between "2011-06-01" and "2011-08-31" and an.ward="01"
group by an.spclty
จะหายอด IPD1 แยกตามแผนก นับแยก ชาย หญิงครับ
;D
-
ไม่ค่อยชำนาญครับ รู้แต่ว่ารูปแบบคำสั่งไม่ถูกครับ.. ;D
-
ไม่ค่อยชำนาญครับ รู้แต่ว่ารูปแบบคำสั่งไม่ถูกครับ.. ;D
ลอง
select if(sex=1,"ชาย","หญิง"),sex,i.spclty,sp.name,count(a.an) as total from an_stat a
left outer join ipt i on i.an=a.an
left outer join spclty sp on sp.spclty=i.spclty
where i.dchdate between "2011-06-01" and "2011-08-31" and a.ward="01"
group by i.spclty
-
select a.spclty,s.name,sum(a.sex=1) Male,sum(a.sex=2) FeMale
from an_stat a
left join spclty s on a.spclty=s.spclty
where a.regdate between "2011-10-01" and "2012-09-30"
group by a.spclty
-
select a.spclty,s.name,sum(a.sex=1) Male,sum(a.sex=2) FeMale
from an_stat a
left join spclty s on a.spclty=s.spclty
where a.regdate between "2011-10-01" and "2012-09-30"
group by a.spclty
เอ่อ !!!!1 นับยอด ผมว่า ไม่ควรจะนับจากวันรับ admit ครับ...
ยอดคนไข้ประจำเดือน ของ IPD ควรนับยอดจาก วันจำหน่าย dchdate ครับ
นับจำนวน คนไข้ น่าจะ count มากกว่า sum นะครับ..
หรือผมสับสน.. ??? ??? ??? ??? ???
-
ไม่ค่อยชำนาญครับ รู้แต่ว่ารูปแบบคำสั่งไม่ถูกครับ.. ;D
ลอง
select if(sex=1,"ชาย","หญิง"),sex,i.spclty,sp.name,count(a.an) as total from an_stat a
left outer join ipt i on i.an=a.an
left outer join spclty sp on sp.spclty=i.spclty
where i.dchdate between "2011-06-01" and "2011-08-31" and a.ward="01"
group by i.spclty
ถูกต้องตามนี้ครับ
-
select if(sex=1,"ชาย","หญิง"),sex,i.spclty,sp.name,count(a.an) as total from an_stat a
left outer join ipt i on i.an=a.an
left outer join spclty sp on sp.spclty=i.spclty
where i.dchdate between "2011-06-01" and "2011-08-31" and a.ward="01"
group by i.spclty
ถ้าแบบนี้มันจะมารวมกันไม่แยกชาย/หญิงครับ โดยเจอเพศไหนก่อนจะแสดงข้อมูลรวมในเพศนั้น
ต้องเพิ่ม sex ใน group by เป็น
select if(sex=1,"ชาย","หญิง"),sex,i.spclty,sp.name,count(a.an) as total from an_stat a
left outer join ipt i on i.an=a.an
left outer join spclty sp on sp.spclty=i.spclty
where i.dchdate between "2011-06-01" and "2011-08-31" and a.ward="01"
group by i.spclty, sex
ผลลัพธ์จึงจะแยกตามเพศอีกทีครับ แต่จะแสดงใน 2 บรรทัด
ส่วนของท่านวรเวทย์ (เซียน SQL ตัวจริง)
select a.spclty,s.name,sum(a.sex=1) Male,sum(a.sex=2) FeMale
from an_stat a
left join spclty s on a.spclty=s.spclty
where a.regdate between "2011-10-01" and "2012-09-30"
group by a.spclty
จะแสดงออกมาในบรรทัดเดียวกันทั้งสองเพศ แยกเป็น 2 column ครับ
ปล. ตัวอย่างตามรูป ผมอนุมานนับตาม regdate ไปก่อน ถูก/ผิด แก้ไขได้ตามต้องการ (เช่น เปลี่ยนเป็น dchdate)
-
ขอบคุณครับ..พี่โก้..และอ. วรเวท woravet...
:o
ได้ความรู้เพิ่มแล้วครับ...ต้องรีบนำไปใช้บ่อย ๆ เด๋วจะลืมอีก... :) :) :) :)