我有两张桌子tblStudent和tblTranscript。我想计算GPA>3.5的学生的百分比。我创建了两个查询,给出了GPA总学生数和GPA>3.5总学生数。
我不知道如何把这两张表分开,再乘以100,得到平均绩点>3.5的学生的百分比。
我的问题是:
平均绩点>3.5
SELECT COUNT (GPA) FROM (
SELECT avg(Grade) as GPA, studentID from tblTranscript
GROUP BY studentID HAVING avg(Grade) > 3.5)
GPA总计数
SELECT COUNT (GPA) FROM (
SELECT avg(Grade) as GPA, StudentID FROM tblTranscript
GROUP BY StudentID)
解决方案:
SELECT
(COUNT (GPA)
/
(SELECT COUNT(GPA) FROM (SELECT avg(Grade) as GPA, StudentID FROM tblTranscript GROUP BY StudentID)) * 100) as PercentageOfStudents
FROM (SELECT avg(Grade) as GPA, studentID from tblTranscript GROUP BY studentID HAVING avg(Grade) > 3.5)
最佳答案
试试这个,
SELECT
(COUNT (studentID)/(SELECT COUNT(studentID) FROM tblTranscript)) as percentageOfStudents
FROM (
SELECT avg(Grade) as GPA, studentID from tblTranscript
GROUP BY studentID HAVING avg(Grade) > 3.5)
希望它能像你期望的那样工作
关于mysql - 如何从两个表中计算GPA> 3.5的百分比?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40992226/