![](/img/trans.png)
[英]mysql - Grouping together rows where Count() less than percentage of total
[英]Calculate percentage of total rows where criteria is true
我想計算滿足某些條件的行數,並將其計算為總行數的百分比(在這種情況下,計算所有ID)。
像這樣:
((select count(error_flag) from MY_TABLE where error_flag == "TRUE" / count(id) from MY_TABLE) * 100) as "% ERROR"
請嘗試以下操作:
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;
問候,
詹姆士
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.