SQL COUNT And SUM -
i have table having table done_by
var_id| var_name| q1_by |q2_by|q3_by|q4_by 1 | abc | me | me |me |you 2 | cba | me | me |you |you 3 | abd | me | |you |me
the result want get total of me , value me=7 you=5
have done count, cant count 'me' each column
you can conditional aggregation like:
select sum(case when q1_by = 'me' 1 else 0 end + case when q2_by = 'me' 1 else 0 end + case when q3_by = 'me' 1 else 0 end + case when q4_by = 'me' 1 else 0 end) me , sum(case when q1_by = 'you' 1 else 0 end + case when q2_by = 'you' 1 else 0 end + case when q3_by = 'you' 1 else 0 end + case when q4_by = 'you' 1 else 0 end) tablename
Comments
Post a Comment