mysql - Optimizing percentage calculation SQL query -
i have table this:
------------------ sl no | error type ------------------ 1 | err1 ------------------ 2 | err2 ------------------ 3 | err1 ------------------ 4 | err2 ------------------ 5 | err2 ------------------ 6 | err3 ------------------
result want:
--------------------------- error type | count | % --------------------------- err1 | 2 | 33.33 --------------------------- err2 | 3 | 50 --------------------------- err3 | 1 | 16.66 ---------------------------
i'm using next query above result:
select error error, count(*) count, 100* count(*)/(select count(*) logs) percentage logs grouping error;
is optimized task? if not, other efficient ways can information?
this query perform count(*)
per row. i'd query once, , cross bring together individual counts:
select a.error, a.cnt, 100 * a.cnt / b.total_count percentage (select error, count(*) cnt logs grouping error) cross bring together (select count(*) total_count logs) b
mysql sql aggregate-functions
No comments:
Post a Comment