2015-03-19 64 views
0

我有兩個表...獲取子計數隨着平均和計數

MODULES    ENROLMENTS 
GroupNo    StudentNo 
Title     GroupNo 
Tutor     CourseworkMark 
DayNo     ExamMark 
Time 
Room 
Semester 

我想創建一個顯示在課程和考試,每個模塊還計數中獲得的平均標記的看法達到> 70,60-69,50-59,40-49和<的學生人數40.這可能嗎?

我的平均分與摸索出...

SELECT Title, 
     AVG(CourseworkMark) AS AverageCoursework, 
     AVG(ExamMark) AS AverageExam 
FROM tblModules INNER JOIN tblEnrolments 
    ON tblModules.GroupNo = tblEnrolments.GroupNo 
GROUP BY Title; 

回答

1

您可以使用一個總結CASE表達式來做到這一點;

SELECT Title, 
     AVG(CourseworkMark) AS AverageCoursework, 
     AVG(ExamMark) AS AverageExam, 
     SUM(CASE WHEN CourseworkMark > 70 THEN 1 ELSE 0 END) AS CourseworkMarkOver70, 
     SUM(CASE WHEN CourseworkMark BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS CourseworkMarkOver60To69, 
     SUM(CASE WHEN CourseworkMark BETWEEN 50 and 59 THEN 1 ELSE 0 END) AS CourseworkMarkOver50To59, 
     SUM(CASE WHEN CourseworkMark BETWEEN 40 and 49 THEN 1 ELSE 0 END) AS CourseworkMarkOver40To49, 
     SUM(CASE WHEN CourseworkMark < 40 THEN 1 ELSE 0 END) AS CourseworkMarkUnder40 
FROM tblModules INNER JOIN tblEnrolments 
    ON tblModules.GroupNo = tblEnrolments.GroupNo 
GROUP BY Title;