ตัวอย่างการใช้ If ร่วมกับ COUNT
DateTime : 2011-06-29 13:12:13
Post By : decepticons
IP Address : 203.113.118.78
select Ga.G1_den_name ,
count( if( vn.aid="321101" ,dt.hn, null ) )as count_all_t_lamduan,
count( DISTINCT if( vn.aid="321101" ,dt.hn, null ) )as count_patient_t_lamduan,
count( DISTINCT if( vn.aid="321101" and vn.count_in_year="0" ,dt.hn, null ) )as count_in_year_t_lamduan,
count( if( vn.aid like "3211%" and vn.aid<> "321101" ,dt.hn, null ) )as count_all_not_t_lamduan,
count( DISTINCT if( vn.aid like "3211%" and vn.aid<> "321101" ,dt.hn, null ) )as count_patient_not_t_lamduan,
count( DISTINCT if( vn.aid like "3211%" and vn.aid<> "321101" and vn.count_in_year="0" ,dt.hn, null ) )as count_in_year_not_t_lamduan,
count( if( vn.aid like "3211%" ,dt.hn, null ) )as count_all_a_lamduan,
count( DISTINCT if( vn.aid like "3211%" ,dt.hn, null ) )as count_patient_a_lamduan,
count( DISTINCT if( vn.aid like "3211%" and vn.count_in_year="0" ,dt.hn, null ) )as count_in_year_a_lamduan,
count( if( vn.aid not like "3211%" ,dt.hn, null ) )as count_all_not_a_lamduan,
count( DISTINCT if( vn.aid not like "3211%" ,dt.hn, null ) )as count_patient_not_a_lamduan,
count( DISTINCT if( vn.aid not like "3211%" and vn.count_in_year="0" ,dt.hn, null ) )as count_in_year_not_a_lamduan,
count(dt.hn) as count_all, count(DISTINCT dt.hn) as count_patient,count( distinct if( vn.count_in_year="0",dt.hn, null ) )as count_in_year
FROM dtmain dt
LEFT OUTER JOIN vn_stat vn on vn.vn=dt.vn
LEFT OUTER JOIN Group_Age Ga on Ga.AGE=vn.age_y
where dt.vstdate between "2011-05-01" and "2011-06-31"
group by Ga.G1_den
Message !!
หากต้องการตอบกระทู้ Webboard กรุณาเข้าใช้ระบบก่อนนะค่ะ..
กระทู้ตอบกลับ
ขอบคุณครับพี่
Post โดย : ranja Date: 2011-06-29 18:21:56 ip: 203.113.116.118