I have a table like this:
------------------
sl no | Error type
------------------
1 | err1
------------------
2 | err2
------------------
3 | err1
------------------
4 | err2
------------------
5 | err2
------------------
6 | err3
------------------
Result I want:
---------------------------
Error type | Count | %
---------------------------
err1 | 2 | 33.33
---------------------------
err2 | 3 | 50
---------------------------
err3 | 1 | 16.66
---------------------------
I'm using following query to get above result:
select
error as Error,
count(*) as Count,
100* count(*)/(select count(*) from logs) as Percentage
from logs
group by error;
Is it optimized for the task? If not, what are some other efficient ways I can get that information?
This query will perform the count(*) per row. I'd query it once, and cross join it to the individual counts:
SELECT a.error, a.cnt, 100 * a.cnt / b.total_count AS percentage
FROM (SELECT error, COUNT(*) as cnt
FROM logs
GROUP BY error) a
CROSS JOIN (SELECT COUNT(*) AS total_count FROM logs) b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With