我需要计算前 20 个失败用户的百分比,在 spring java 和 hibernate 中需要它。
+--------------+--------+------------+
| id | result | code | techUser_id
+--------------+--------+------------+
| 1 | fail | 23442 | 2
| 2 | fail | 56432 | 5
| 3 | fail | 98745 | 2
| 4 | fail | 65478 | 5
| 5 | fail | 36448 | 2
| 6 | fail | 87745 | 5
+--------------+--------+------------+
预期输出:前 20 名的列表失败,每个用户占记录总数的最大百分比
我不确定查询应该是什么,所以请帮助我找到解决方案。
我有一些东西,我知道它不正确:--
select techUser_id, count( * ),(SELECT COUNT( * ) from inspection) * 100 ,
count( * )/(SELECT COUNT( * ) from inspection) * 100 as perc
from inspection
where techUser_id != ''
and inspectionResult ='FAIL'
group by techUser_id
order by perc limit 20;
得到这个结果
谢谢
执行此查询以进行检查
select techUser_id, result, count(*) num
from inspection
group by techUser_id, result
order by num
最佳答案
试试这个:
SELECT techUser_id, COUNT(*) AS Total , (COUNT(*) / (SELECT COUNT(*)
FROM inspection WHERE result='fail')) * 100 AS 'list of top 20 failed with max',
FROM inspection
WHERE result='fail'
GROUP BY techUser_id;
关于MySQL从一张表计算百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37645015/