I have two tables, one for Grades
and one for Students
. The Grades
table has a column Student
which is a foreign key referencing to Student.Id
.
Students have multiple grades at different subjects and some of the students have more than one grade at a subject. If a student has more than one grade at a subject, how can I do the general average of those grades using only the last grade he got at each subject, rather than all grades?
For example, if a student has Maths=3,History=5,Physics=4,History=7
I want to do the average using only Maths(3),Physics(4) and the last History(7), without the History(5).
As you can see in that image, i have a Students table with Name, Group, Location and a Grades table that have those grades. I want to do the average of those grades but in case there are more than 1 grad at the same subject the last grade at that subject should be used. In this case the avg should be AVG of French(9) + Maths(10) + the last grade taken at physics which is 5.
I'm sorry i just cant figure out what to do in this case. I am new to using relational databases, foreign keys etc.
So far i thought about using de Id to differentiate the grades. So for Physics 4 which is the first grade of that student it has the id of 1 and the Physics 5 which is the last grade he got at Physics it has the id of 7. So to get the last grade of that subject it should take the one with the higher id number but i can't figure out how to do it.