我正在编写查询,以查找在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
任何人都可以帮忙吗?
答案 0 :(得分: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
答案 1 :(得分: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