计算每个用户的平均值

时间:2015-08-02 21:35:14

标签: mysql

我需要使用mysql查询来计算认证百分比,并且想知道是否有人可以给我指点。以下是一些示例集:

User 1: Failed: 1; Passed: 1 == Certified: Yes
User 2: Failed: 0; Passed: 1 == Certified: Yes
User 3: Failed: 3; Passed: 0 == Certified: No
User 4: Failed: 2; Passed: 1 == Certified: Yes
User 5: Failed: 4; Passed: 0 == Certified: No

认证百分比= 60%

这就是数据在DB中的样子:

CourseName  StudentName StudentEmail    Status  
Course A    BB Tester   b@b.com         Pass
Course B    BB Tester   b@b.com         Fail
Course B    AA Tester   a@a.com         Fail
Course B    AA Tester   a@a.com         Fail
Course B    AA Tester   a@a.com         Pass
Course C    CC Tester   c@c.com         Pass
Course D    DD Tester   d@d.com         Fail

我应该如何构成查询。它必须在用户级别完成,所以我怀疑子查询?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

SELECT ((SELECT count(*) FROM `your_data` WHERE `status`='Pass') / count(*))*100 FROM your_data

或此格式化版本:

SELECT CONCAT(ROUND((SELECT count(*) FROM `your_data` WHERE `status`='Pass') / count(*))*100,2),'%') FROM `your_data`