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 に答える