学生在一堂课中从每个科目中获得的分数生成的位置

时间:2019-01-08 11:52:19

标签: c# sql asp.net sql-server database

我有一个学生考试成绩表,该表现在存储学生的成绩,我想根据学生的最高成绩来计算职位。

select er.student_id, first_name, class_id, subject_id, subsubject_id,
       sum(total_marks) totalmarks, sum(marks_obtained) obtainedmarks, (0) Position
from Exam_Results er inner join
     student s
     on er.student_id = s.student_id
where class_id = 272     
group by er.student_id, session_id, shift_id, class_id, subclass_id, term_id, catagory_id, exam_type_id, subject_id, subsubject_id, first_name
order by student_id desc

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以尝试以下查询。

Select er.student_id, first_name, class_id, subject_id, subsubject_id,
t1.totalmarks, t1.obtainedmarks, 
DENSE_RANK() OVER (ORDER BY totalmarks) as position 
from Exam_Results er 
inner join student s on er.student_id = s.student_id
inner join(select student_id, sum(total_marks) totalmarks, 
sum(marks_obtained) obtainedmarks from Exam_Results group by student_id) t1
on t1.student_id=s.student_id
where class_id = 272

答案 1 :(得分:0)

选择er.student_id,first_name,class_id,subject_id,subsubject_id,        sum(total_marks)个总标记,sum(marks_obtained)获得的标记,DENSE_RANK()OVER(ORDER BY sum(marks_obtained)desc)作为位置 来自Exam_Results的内部联接      学生们      在er.student_id = s.student_id上 其中class_id = 272
按er.student_id,session_id,shift_id,class_id,subclass_id,term_id,catagory_id,exam_type_id,subject_id,subsubject_id,first_name分组 按student_id desc排序

请尝试此查询。我认为这应该可以解决您的问题,因为我本人已经尝试过以达到预期效果。

答案 2 :(得分:0)

如果只希望基于聚合进行排名,则不需要子查询或CTE。只要做:

select er.student_id, first_name, class_id, subject_id, subsubject_id,
       sum(total_marks) as totalmarks, sum(marks_obtained) as obtainedmarks,
       dense_rank() over (order by sum(total_marks) desc) as Position
from Exam_Results er inner join
     student s
     on er.student_id = s.student_id
where class_id = 272     
group by er.student_id, session_id, shift_id, class_id, subclass_id, term_id, catagory_id, exam_type_id, subject_id, subsubject_id, first_name
order by student_id desc;