There’s one table named StudentScore which has fields of: Score, CourseID, StudentID and Semester. The later three ones are the primary keys.
I want to write a stored procedure to get the average score of each student. But the rule is quite complex and I don’t know how to express it in one query. Nested query should be avoided if is possible.
Here is the rule:
If one student take a course for more than once, only the last score should be calculated.
For example, there’re following data:
StudentID | CourseID | Semester | Score
1 1 1 80
1 2 1 40
1 3 1 60
1 2 2 50
1 3 2 20
2 1 1 90
The stored procedure should return:
StudentID | AvgScore
1 50 // which is avg(80, 50, 20)
2 90
Please suggest stored procedure as efficient as possible. Thanks!
If you want something rounded to certain decimal places, maybe: