[英]Calculate percentage of total rows where criteria is true
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). 我想计算满足某些条件的行数,并将其计算为总行数的百分比(在这种情况下,计算所有ID)。
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 詹姆士
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.