我有两个表tblStudent和tblTranscript。我想计算拥有GPA>的学生百分比3.5。我创建了两个查询,这些查询给出了拥有GPA的学生总数和拥有GPA的学生总数> 3.5。
我不知道如何划分这两个表并将它们乘以100以获得具有GPA>的学生的百分比。 3.5。
我的疑问是:
GPA> 3.5
SELECT COUNT (GPA) FROM (
SELECT avg(Grade) as GPA, studentID from tblTranscript
GROUP BY studentID HAVING avg(Grade) > 3.5)
TotalCountWithGPA
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)
答案 0 :(得分:1)
试试这个,
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)
希望它会像你期待的那样工作
答案 1 :(得分:1)
我现在明白你的问题。由于找不到页面错误,我无法编辑我的最后一个答案。无论如何,我已经编辑了我的最后一个答案并发布了。检查它是否有效,
SELECT
(COUNT(GPA)/(SELECT COUNT(GPA) FROM (SELECT avg(Grade) as GPA, studentID from tblTranscript)) * 100) as percentageOfStudents
FROM (
SELECT avg(Grade) as GPA, studentID from tblTranscript
GROUP BY studentID HAVING avg(Grade) > 3.5)
答案 2 :(得分:0)
MySQL还是Access?
对于MySQL,您可以使用:
SELECT COUNT(GPA > 3.5)/COUNT(GPA)*100 as HIGH_GPA_PERC
FROM(SELECT avg(Grade) as GPA, StudentID
FROM tblTranscript
GROUP BY StudentID) t