sql server - How to group values of one column based on condition in t-sql? -


my table has doctor id, visit id (one doctor can have several visits) , visit cause, looks this:

doctor_uid | visit_uid | visit_cause 11-11-11    22-22-11      1 22-22-22    44-44-22      2 11-11-11    23-23-23      1 

i need count visits each doctor, based on visit_cause (it should equal '1'). before count visits each doctor without condition of visit_cause, must have in query too:

select     v.vra_uid,     count(v.visit_uid) on (partition v.vra_uid) number_of_visits visits v  

it returns:

doctor_uid | number_of_visits      11-11-11    2          22-22-22    1      

how can count visits, have visit_cause = '1' each doctor? like:

    doctor_uid | number_of_visits | visits_with_cause_equal_1     11-22-33    2                           2     22-22-22    1                           0 

thanks in advance!

this should work:

select v.vra_uid, count(v.visit_uid) number_of_visits , sum(case when v.visit_cause = 1 1 else 0 end) visits_with_cause_equal_1 visits v  group v.vra_uid 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -