I want to count the number of rows that meet some criteria and calculate that as a percentage of total number of rows (in this case counting all of the id's).
Something like this:
((select count(error_flag) from MY_TABLE where error_flag == "TRUE" / count(id) from MY_TABLE) * 100) as "% ERROR"
Please try the following:
CREATE TABLE errorTest (`error_flag` ENUM('TRUE','FALSE'));
INSERT INTO errorTest VALUES ('FALSE'), ('TRUE'), ('TRUE');
SELECT SUM(IF(error_flag = "TRUE", 1, 0)) / COUNT(*) `% ERROR` FROM errorTest;
Regards,
James
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.