使用以下代码,我没有得到总百分比,而是每个不是“U”的值的百分比。
如何更改代码以获得总百分比?
SELECT grade, (COUNT(grade)* 100 / (SELECT COUNT(*) FROM studied)) AS Score
FROM studied
WHERE grade !='U'
GROUP BY grade
使用以下代码,我没有得到总百分比,而是每个不是“U”的值的百分比。
如何更改代码以获得总百分比?
SELECT grade, (COUNT(grade)* 100 / (SELECT COUNT(*) FROM studied)) AS Score
FROM studied
WHERE grade !='U'
GROUP BY grade
以下代码将返回一个值列表:grade和score。
Grade将显示除 U 之外的所有可用成绩。Score值将显示相应成绩(包括U)的总百分比。如果您想将分数作为不包括U 的分数的百分比,则以下 SQL 应该可以使用
SELECT grade, (COUNT(grade) * 100 / (SELECT COUNT(*) FROM studied WHERE grade !='U')) AS Score
FROM studied
WHERE grade !='U'
GROUP BY grade
下面的截图显示了测试数据和查询结果
编辑
从您的最后评论看来,这个 SQL 应该可以完成工作:
SELECT (COUNT(grade) * 100 / (SELECT COUNT(*) FROM student)) AS Score
FROM Student
WHERE grade !='U'
它将返回除 U 之外的所有成绩的总百分比。使用屏幕截图中的数据集,它将是 92(即 92%)