I am running queries to do a count of data (How many males/females, how many speakers of spanish/french/etc).
All is fine as long as the case statement uses an = sign. If I need an in clause (CASE WHEN EnglisProf IN('A', 'B') then count(EnglisProf) else 0 end as EnglisProf1) then it will only count IF there are all 'A' or all 'B'.
I have worked around this by splitting them out to ='A', ='B' and then using a sum.
This would be ok, but I have one field can hold 12 different codes. Out of these, I need to count up 9 of these codes.
Is there a more eloquent solution then counting each code separately and then summing it? This seems a bit cumbersome.
sum(EnglisProfA + EnglisProfB + EnglisProfC + EnglisProfD)
CASE WHEN EnglisProf ='A' then count(EnglisProf) else 0 end as EnglisProfA
CASE WHEN EnglisProf ='B' then count(EnglisProf) else 0 end as EnglisProfB
CASE WHEN EnglisProf ='C' then count(EnglisProf) else 0 end as EnglisProfC
CASE WHEN EnglisProf ='D' then count(EnglisProf) else 0 end as EnglisProfD
Thanks for any ideas about this!
J
All is fine as long as the case statement uses an = sign. If I need an in clause (CASE WHEN EnglisProf IN('A', 'B') then count(EnglisProf) else 0 end as EnglisProf1) then it will only count IF there are all 'A' or all 'B'.
I have worked around this by splitting them out to ='A', ='B' and then using a sum.
This would be ok, but I have one field can hold 12 different codes. Out of these, I need to count up 9 of these codes.
Is there a more eloquent solution then counting each code separately and then summing it? This seems a bit cumbersome.
sum(EnglisProfA + EnglisProfB + EnglisProfC + EnglisProfD)
CASE WHEN EnglisProf ='A' then count(EnglisProf) else 0 end as EnglisProfA
CASE WHEN EnglisProf ='B' then count(EnglisProf) else 0 end as EnglisProfB
CASE WHEN EnglisProf ='C' then count(EnglisProf) else 0 end as EnglisProfC
CASE WHEN EnglisProf ='D' then count(EnglisProf) else 0 end as EnglisProfD
Thanks for any ideas about this!
J