mysql - More efficient way of counting occurrences of different column values? -
mysql - More efficient way of counting occurrences of different column values? -
i've seen related questions, didn't seem same situation, help appreciated.
i have current query:
select count(case when training_enhancments.reason_id = 0 1 end) '0count', count(case when training_enhancments.reason_id = 1 1 end) '1count', count(case when training_enhancments.reason_id = 2 1 end) '2count', count(case when training_enhancments.reason_id = 3 1 end) '3count', count(case when training_enhancments.reason_id = 8 1 end) '4count', ... count(case when training_enhancments.reason_id = 40 1 end) '40count', claims claims inner bring together users users on claims.surveyor_id = users.user_id inner bring together insurers insurers on claims.insurer_id = insurers.insurer_id left bring together training_enhancments training_enhancments on claims.claim_id = training_enhancments.claim_id claims.claim_cancelled_id <= 0 , claims.date_completed between '2014-10-01 00:00:00' , '2014-10-31 23:59:59' grouping claims.surveyor_id;
how can rewrite count() statements more efficient / readable? / while loop, i'm not sure exist in sql.
thanks.
try grouping training_enhancments.reason_id
, in application code, can utilize for/while loop display 40 counts.
select claims.surveyor_id, training_enhancments.reason_id, count(*) claims claims inner bring together users users on claims.surveyor_id = users.user_id inner bring together insurers insurers on claims.insurer_id = insurers.insurer_id left bring together training_enhancments training_enhancments on claims.claim_id = training_enhancments.claim_id claims.claim_cancelled_id <= 0 , claims.date_completed between '2014-10-01 00:00:00' , '2014-10-31 23:59:59' grouping claims.surveyor_id, training_enhancments.reason_id;
mysql sql
Comments
Post a Comment