0

我正在编写查询以查找分数在 1-50 和 50-70 和 70-100 之间的学生人数我试图解决它,但我所做的只是检索分数在 1-50 之间的学生,我需要检索其他值(50-70 和 70-100 之间)

 ;with StudentMarks as
(
SELECT     Users.UserID,classes.Name Class, courses.Name AS Course, Sum(UserExams.StudentMark) Tot
FROM         ClassCourses INNER JOIN classes ON ClassCourses.ClassID = classes.ClassID 
                      INNER JOIN courses ON ClassCourses.CourseID = courses.CourseID 
                      INNER JOIN Exams ON ClassCourses.ClassID = Exams.ClassID AND ClassCourses.CourseID = Exams.CourseID 
                      INNER JOIN SectionsClasses ON classes.ClassID = SectionsClasses.ClassID 
                      INNER JOIN UserExams ON Exams.ExamID = UserExams.ExamID 
                      INNER JOIN Users ON SectionsClasses.SectionID = Users.SectionID AND SectionsClasses.ClassID = Users.ClassID AND UserExams.UserID = Users.UserID
    Group by    Users.UserID, classes.Name ,courses.Name              
)
SELECT  Class,Course,
       count(UserID)'1-50'

from StudentMarks 
where tot between 1 and 50
Group by Class,Course

有人可以帮忙吗?

4

2 回答 2

0

您可以使用case

;with studentmarks as (....)
select 
     class, 
     course, 
     case  when tot<=49 then '0-49' 
           when tot>=71 then '71+' 
           else '50-70' end as gp,
     count(userid)
from studentmarks
group by 
     class, 
     course, 
     case when tot<50 then '0-49' when tot>70 then '71+' else '50-70' end 
于 2013-10-20T14:27:00.617 回答
0

这个工作不错

select Class,Course,SUM(case when tot between 1 and 50 then 1 else 0 end )'1-50'
                        ,SUM(case when tot between 50 and 70 then 1 else 0 end )'50-70'
                        ,SUM(case when tot between 70 and 100 then 1 else 0 end )'70-100'
FROM
(
SELECT     Users.UserID,classes.Name Class, courses.Name AS Course, Sum(UserExams.StudentMark) Tot

FROM         ClassCourses INNER JOIN classes ON ClassCourses.ClassID = classes.ClassID 
                      INNER JOIN courses ON ClassCourses.CourseID = courses.CourseID 
                      INNER JOIN Exams ON ClassCourses.ClassID = Exams.ClassID AND ClassCourses.CourseID = Exams.CourseID 
                      INNER JOIN SectionsClasses ON classes.ClassID = SectionsClasses.ClassID 
                      INNER JOIN UserExams ON Exams.ExamID = UserExams.ExamID 
                      INNER JOIN Users ON SectionsClasses.SectionID = Users.SectionID AND SectionsClasses.ClassID = Users.ClassID AND UserExams.UserID = Users.UserID
    WHERE courses.CourseID =@pCourseID
    Group by    Users.UserID, classes.Name ,courses.Name
)AS SQ
GROUP BY Class,Course
于 2013-10-21T06:20:50.740 回答