BMS-HOSxP Community

HOSxP => Report Exchange => ข้อความที่เริ่มโดย: yokyai ที่ เมษายน 23, 2012, 12:03:50 PM

หัวข้อ: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yokyai ที่ เมษายน 23, 2012, 12:03:50 PM
count(distinct(if จะได้ยอดมากกว่า sum(if อยู่ 1 รายการครับ และนับยอดปกติปรากฎว่ายอดที่นับจาก sum(if ถูกต้องครับ ไม่ทราบว่าจะแก้ไขอย่างไรครับขอบคุณครับ
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yutbit ที่ เมษายน 24, 2012, 11:43:32 AM
ขอรายละเอียดด้วยครับ เช่น SQL ทั้งหมดเป็นยังไง
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yokyai ที่ เมษายน 24, 2012, 18:57:44 PM
ตัวอย่าง
select count(distinct if(month(vstdate)=10,vn,0)) as "M10"
,count(distinct if(month(vstdate)=11,vn,0)) as "M11"
,count(distinct if(month(vstdate)=12,vn,0)) as "M12"
,sum(if(month(vstdate)=10,1,0)) as "M101"
,sum(if(month(vstdate)=11,1,0)) as "M111"
,sum(if(month(vstdate)=12,1,0)) as "M121"
from vn_stat
where vstdate between "2011-10-01" and "2011-12-31"

ผลลัพธ์ที่ได้ ถ้าใช้ count(distinct จำนวนจะมากกว่า sum(if อยู่ 1 ซึ่งทดสอบการนับยอดโดยใช้คำสั่งปกติแล้วยอดที่ถูกต้องคือ sum(if ครับ
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: mashimaro ที่ เมษายน 24, 2012, 23:53:16 PM
ตัวอย่าง
select count(distinct if(month(vstdate)=10,vn,0)) as "M10"
,count(distinct if(month(vstdate)=11,vn,0)) as "M11"
,count(distinct if(month(vstdate)=12,vn,0)) as "M12"
,sum(if(month(vstdate)=10,1,0)) as "M101"
,sum(if(month(vstdate)=11,1,0)) as "M111"
,sum(if(month(vstdate)=12,1,0)) as "M121"
from vn_stat
where vstdate between "2011-10-01" and "2011-12-31"

ผลลัพธ์ที่ได้ ถ้าใช้ count(distinct จำนวนจะมากกว่า sum(if อยู่ 1 ซึ่งทดสอบการนับยอดโดยใช้คำสั่งปกติแล้วยอดที่ถูกต้องคือ sum(if ครับ

ผมขอโจทย์เลยได้มั้ยครับ มองตามคุณแล้วผมว่ามันยังแปลก ๆ อยู่น่ะ Query ครับ ช่วยบอกความต้องการมาเลยดีกว่าครับ จะได้มีคนช่วยเสนอวิธีการแก้ปัญหาให้คุณได้ครับ
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yokyai ที่ เมษายน 25, 2012, 08:57:11 AM
เพียงแต่ยกตัวอย่างให้เห็นเฉยๆครับว่า กรณีที่เราต้องการนับจำนวนคนไข้จากตารางเดียวกัน เงื่อนไขเดียวกัน ทำไมยอดออกมาไม่ตรงกัน
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: chitvichai ที่ เมษายน 25, 2012, 10:18:03 AM
เพียงแต่ยกตัวอย่างให้เห็นเฉยๆครับว่า กรณีที่เราต้องการนับจำนวนคนไข้จากตารางเดียวกัน เงื่อนไขเดียวกัน ทำไมยอดออกมาไม่ตรงกัน
ก็เพราะ count(distinct if(month(vstdate)=10,vn,0)) มันนับ 0 ด้วยครับ เลยเกิน มา 1
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yokyai ที่ เมษายน 25, 2012, 10:24:36 AM
เพียงแต่ยกตัวอย่างให้เห็นเฉยๆครับว่า กรณีที่เราต้องการนับจำนวนคนไข้จากตารางเดียวกัน เงื่อนไขเดียวกัน ทำไมยอดออกมาไม่ตรงกัน
ก็เพราะ count(distinct if(month(vstdate)=10,vn,0)) มันนับ 0 ด้วยครับ เลยเกิน มา 1
ถ้าอย่างนั้นเราก็ต้องเปลี่ยน 0 เป็น null หรือเปล่าครับ
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: yokyai ที่ เมษายน 25, 2012, 10:32:11 AM
select count(distinct(if(lo.lab_items_code=231,lh.vn,null))) as "fbs_all"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >110,lh.vn,null))) as "fbs110up"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result <126,lh.vn,null))) as "FBSLess126"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >=126,lh.vn,null))) as "FBSMoreOrEqual126"
,count(distinct(if(lo.lab_items_code=316,lh.vn,null))) as "fpgall"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result >110,lh.vn,null))) as "fpg110up"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result<126,lh.vn,null))) as "FPGLess126"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result>=126,lh.vn,null))) as "FPGMoreOrEqual126"
,count(distinct(if(lo.lab_items_code=117,lh.vn,null))) as "chol_all"
,count(distinct(if(lo.lab_items_code=117 and lo.lab_order_result <=200,lh.vn,null))) as "chol_lessequal200"
,count(distinct(if(lo.lab_items_code=118,lh.vn,null))) as "tg_all"
,count(distinct(if(lo.lab_items_code=118 and lo.lab_order_result <=150,lh.vn,null))) as "tg_lessequal150"
,count(distinct(if(lo.lab_items_code=119,lh.vn,null))) as "hdl_all"
,count(distinct(if(lo.lab_items_code=119 and lo.lab_order_result >=40,lh.vn,null))) as "hdl_more40"
,count(distinct(if(lo.lab_items_code=120,lh.vn,null))) as "ldl_all"
,count(distinct(if(lo.lab_items_code=120 and lo.lab_order_result <100,lh.vn,null))) as "ldl_less100"
,count(distinct(if(lo.lab_items_code=99,lh.vn,null))) as "bun_all"
,count(distinct(if(lo.lab_items_code=99 and lo.lab_order_result <=25,lh.vn,null))) as "bun_25"
,count(distinct(if(lo.lab_items_code=100,lh.vn,null))) as "creatinin_all"
,count(distinct(if(lo.lab_items_code=100 and lo.lab_order_result <=1.5,lh.vn,null))) as "creatinin1"
,count(distinct(if(lo.lab_items_code=244,lh.vn,null))) as "UrAl_all"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result="Negative",lh.vn,null))) as "UrAl_Nagative"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result>+1,lh.vn,null))) as "UrAlMore1"
,count(distinct(if(lo.lab_items_code=103,lh.vn,null))) as "Uric_all"
,count(distinct(if(lo.lab_items_code=103 and lo.lab_order_result >7,lh.vn,null))) as "Uric_7up"
,count(distinct(if(lo.lab_items_code=231,lh.hn,null))) as "fbs_allPatient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >110,lh.hn,null))) as "fbs110upPatient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result <126,lh.hn,null))) as "FBSLess126Patient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >=126,lh.hn,null))) as "FBSMoreOrEqual126Patient"
,count(distinct(if(lo.lab_items_code=316,lh.hn,null))) as "fpgallPatient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result >110,lh.hn,null))) as "fpg110upPatient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result<126,lh.hn,null))) as "FPGLess126Patient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result>=126,lh.hn,null))) as "FPGMoreOrEqual126Patient"
,count(distinct(if(lo.lab_items_code=117,lh.hn,null))) as "chol_allPatient"
,count(distinct(if(lo.lab_items_code=117 and lo.lab_order_result <=200,lh.hn,null))) as "chol_lessequal200Patient"
,count(distinct(if(lo.lab_items_code=118,lh.hn,null))) as "tg_allPatient"
,count(distinct(if(lo.lab_items_code=118 and lo.lab_order_result <=150,lh.hn,null))) as "tg_lessequal150Patient"
,count(distinct(if(lo.lab_items_code=119,lh.hn,null))) as "hdl_allPatient"
,count(distinct(if(lo.lab_items_code=119 and lo.lab_order_result >=40,lh.hn,null))) as "hdl_more40Patient"
,count(distinct(if(lo.lab_items_code=120,lh.hn,null))) as "ldl_allPatient"
,count(distinct(if(lo.lab_items_code=120 and lo.lab_order_result <100,lh.hn,null))) as "ldl_less100Patient"
,count(distinct(if(lo.lab_items_code=99,lh.hn,null))) as "bun_allPatient"
,count(distinct(if(lo.lab_items_code=99 and lo.lab_order_result <=25,lh.hn,null))) as "bun_25Patient"
,count(distinct(if(lo.lab_items_code=100,lh.hn,0))) as "creatinin_allPatient"
,count(distinct(if(lo.lab_items_code=100 and lo.lab_order_result <=1.5,lh.hn,null))) as "creatinin1Patient"
,count(distinct(if(lo.lab_items_code=244,lh.hn,null))) as "UrAl_allPatient"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result="Negative",lh.hn,null))) as "UrAl_NagativePatient"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result>+1,lh.hn,null))) as "UrAlMore1Patient"
,count(distinct(if(lo.lab_items_code=103,lh.hn,null))) as "Uric_allPatient"
,count(distinct(if(lo.lab_items_code=103 and lo.lab_order_result >7,lh.hn,null))) as "Uric_7upPatient"
from lab_order  lo
left outer join lab_head lh on lo.lab_order_number=lh.lab_order_number
left outer join clinicmember c on c.hn=lh.hn
left outer join vn_stat v on v.vn=lh.vn
where v.vstdate between "2011-01-01" and "2011-01-15" and c.clinic="002"
ถ้าตามนี้แล้วมีอะไรผิดกฎการประมวลผลหรือไม่ครับ หรือจะทำให้การประมวลผลมีปัญหา มีผลต่อการเปลี่ยนแปลงค่าใดๆตามมาหรือไม่ครับ
หัวข้อ: Re: ประมวลผลระหว่าง count(distinct(if กับ sum(if ไม่ตรง
เริ่มหัวข้อโดย: mashimaro ที่ เมษายน 26, 2012, 01:13:38 AM
select count(distinct(if(lo.lab_items_code=231,lh.vn,null))) as "fbs_all"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >110,lh.vn,null))) as "fbs110up"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result <126,lh.vn,null))) as "FBSLess126"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >=126,lh.vn,null))) as "FBSMoreOrEqual126"
,count(distinct(if(lo.lab_items_code=316,lh.vn,null))) as "fpgall"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result >110,lh.vn,null))) as "fpg110up"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result<126,lh.vn,null))) as "FPGLess126"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result>=126,lh.vn,null))) as "FPGMoreOrEqual126"
,count(distinct(if(lo.lab_items_code=117,lh.vn,null))) as "chol_all"
,count(distinct(if(lo.lab_items_code=117 and lo.lab_order_result <=200,lh.vn,null))) as "chol_lessequal200"
,count(distinct(if(lo.lab_items_code=118,lh.vn,null))) as "tg_all"
,count(distinct(if(lo.lab_items_code=118 and lo.lab_order_result <=150,lh.vn,null))) as "tg_lessequal150"
,count(distinct(if(lo.lab_items_code=119,lh.vn,null))) as "hdl_all"
,count(distinct(if(lo.lab_items_code=119 and lo.lab_order_result >=40,lh.vn,null))) as "hdl_more40"
,count(distinct(if(lo.lab_items_code=120,lh.vn,null))) as "ldl_all"
,count(distinct(if(lo.lab_items_code=120 and lo.lab_order_result <100,lh.vn,null))) as "ldl_less100"
,count(distinct(if(lo.lab_items_code=99,lh.vn,null))) as "bun_all"
,count(distinct(if(lo.lab_items_code=99 and lo.lab_order_result <=25,lh.vn,null))) as "bun_25"
,count(distinct(if(lo.lab_items_code=100,lh.vn,null))) as "creatinin_all"
,count(distinct(if(lo.lab_items_code=100 and lo.lab_order_result <=1.5,lh.vn,null))) as "creatinin1"
,count(distinct(if(lo.lab_items_code=244,lh.vn,null))) as "UrAl_all"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result="Negative",lh.vn,null))) as "UrAl_Nagative"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result>+1,lh.vn,null))) as "UrAlMore1"
,count(distinct(if(lo.lab_items_code=103,lh.vn,null))) as "Uric_all"
,count(distinct(if(lo.lab_items_code=103 and lo.lab_order_result >7,lh.vn,null))) as "Uric_7up"
,count(distinct(if(lo.lab_items_code=231,lh.hn,null))) as "fbs_allPatient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >110,lh.hn,null))) as "fbs110upPatient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result <126,lh.hn,null))) as "FBSLess126Patient"
,count(distinct(if(lo.lab_items_code=231 and lo.lab_order_result >=126,lh.hn,null))) as "FBSMoreOrEqual126Patient"
,count(distinct(if(lo.lab_items_code=316,lh.hn,null))) as "fpgallPatient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result >110,lh.hn,null))) as "fpg110upPatient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result<126,lh.hn,null))) as "FPGLess126Patient"
,count(distinct(if(lo.lab_items_code=316 and lo.lab_order_result>=126,lh.hn,null))) as "FPGMoreOrEqual126Patient"
,count(distinct(if(lo.lab_items_code=117,lh.hn,null))) as "chol_allPatient"
,count(distinct(if(lo.lab_items_code=117 and lo.lab_order_result <=200,lh.hn,null))) as "chol_lessequal200Patient"
,count(distinct(if(lo.lab_items_code=118,lh.hn,null))) as "tg_allPatient"
,count(distinct(if(lo.lab_items_code=118 and lo.lab_order_result <=150,lh.hn,null))) as "tg_lessequal150Patient"
,count(distinct(if(lo.lab_items_code=119,lh.hn,null))) as "hdl_allPatient"
,count(distinct(if(lo.lab_items_code=119 and lo.lab_order_result >=40,lh.hn,null))) as "hdl_more40Patient"
,count(distinct(if(lo.lab_items_code=120,lh.hn,null))) as "ldl_allPatient"
,count(distinct(if(lo.lab_items_code=120 and lo.lab_order_result <100,lh.hn,null))) as "ldl_less100Patient"
,count(distinct(if(lo.lab_items_code=99,lh.hn,null))) as "bun_allPatient"
,count(distinct(if(lo.lab_items_code=99 and lo.lab_order_result <=25,lh.hn,null))) as "bun_25Patient"
,count(distinct(if(lo.lab_items_code=100,lh.hn,0))) as "creatinin_allPatient"
,count(distinct(if(lo.lab_items_code=100 and lo.lab_order_result <=1.5,lh.hn,null))) as "creatinin1Patient"
,count(distinct(if(lo.lab_items_code=244,lh.hn,null))) as "UrAl_allPatient"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result="Negative",lh.hn,null))) as "UrAl_NagativePatient"
,count(distinct(if(lo.lab_items_code=244 and lo.lab_order_result>+1,lh.hn,null))) as "UrAlMore1Patient"
,count(distinct(if(lo.lab_items_code=103,lh.hn,null))) as "Uric_allPatient"
,count(distinct(if(lo.lab_items_code=103 and lo.lab_order_result >7,lh.hn,null))) as "Uric_7upPatient"
from lab_order  lo
left outer join lab_head lh on lo.lab_order_number=lh.lab_order_number
left outer join clinicmember c on c.hn=lh.hn
left outer join vn_stat v on v.vn=lh.vn
where v.vstdate between "2011-01-01" and "2011-01-15" and c.clinic="002"
ถ้าตามนี้แล้วมีอะไรผิดกฎการประมวลผลหรือไม่ครับ หรือจะทำให้การประมวลผลมีปัญหา มีผลต่อการเปลี่ยนแปลงค่าใดๆตามมาหรือไม่ครับ

ผมแนะนำว่า เพื่อพิสูจน์ เงื่อนไขความถูกต้องของ Run Query ตัวนี้ทั้งหมดน่ะครับ ผมว่าต้อง Run Lab ที่ละตัวดูก่อนครับ ว่าค่ามันตรงกับการ Run ที่เดียวทั้งหมดหรือเปล่าครับ
การประผลแต่ละครั้ง จะมีการรายงานจำนวนเวลาในการ Run อยู่แล้วในด้านล่างของ Tool นั้น ๆ ครับ จะได้รู้ว่า Query ของเรามีประสิทธิภาพแค่ไหนครับ