Hi, can anyone help me to write a code for some calculations?
To calculate the final grade for each subject:
1. The grade for 1st attempt is the final grade only if:
-It is the only grade for that subject.
-The only other grade is a Complaint and this has the value 0 (or anything below 10).
-If there is either a Complaint with a grade >=10, a 2nd attempt grade, or an equivalent, the grade for 1st attempt does not count.
2. The grade for 2nd attempt is the final grade if:
-There is not a Complaint with a grade >= 10.
-There is not an equivalent.
3. The grade for a Complaint is the final grade only if:
- It is larger than or equal to 10.
-There is not an equivalent.

Sample of data is here
Subject Year Passing attempt Grade ID name Surname subjectID
Subject 3 2015/2016 1st attempt 0 1125 Anna Marsel 5
Subject 3 2015/2016 Exam Special 0 1125 Anna Marsel 5
Subject 10 2015/2016 1st attempt 11 1125 Anna Marsel 19
Subject 10 2015/2016 1st attempt 0 1125 Anna Marsel 19
Subject 10 2015/2016 1st attempt 11 1125 Anna Marsel 19
Subject 3 2015/2016 Complaint 11 1125 Anna Marsel 5
Subject 2 2015/2016 1st attempt 6 1125 Anna Marsel 3
Subject 2 2015/2016 2nd attempt 10 1125 Anna Marsel 3
Subject 6 2015/2016 1st attempt 12 1125 Anna Marsel 11
Subject 6 2015/2016 1st attempt 0 1125 Anna Marsel 11
Subject 6 2015/2016 equivalent 0 1125 Anna Marsel 11
Subject 8 2015/2016 1st attempt 10 1125 Anna Marsel 15
Subject 9 2015/2016 1st attempt 11 1125 Anna Marsel 17
Subject 9 2015/2016 Complaint 0 1125 Anna Marsel 17
Subject 4 2015/2016 1st attempt 8 1125 Anna Marsel 7
Subject 4 2015/2016 2nd attempt 6 1125 Anna Marsel 7
Subject 4 2015/2016 Complaint 0 1125 Anna Marsel 7
Subject 4 2015/2016 2nd attempt 11 1125 Anna Marsel 7
Subject 4 2015/2016 1st attempt 4 1125 Anna Marsel 7
Subject 1 2015/2016 Complaint 0 1125 Anna Marsel 1
Subject 7 2015/2016 1st attempt 0 1125 Anna Marsel 13
Subject 7 2015/2016 1st attempt 13 1125 Anna Marsel 13
Subject 5 2015/2016 Complaint 0 1125 Anna Marsel 9
Subject 1 2015/2016 Complaint 0 1125 Anna Marsel 1
Subject 5 2015/2016 1st attempt 10 1125 Anna Marsel 9
Subject 1 2015/2016 2nd attempt 6 1125 Anna Marsel 1
Subject 1 2015/2016 1st attempt 7 1125 Anna Marsel 1
Subject 4 2015/2016 1st attempt 6 5187 Juli Kan 8
Subject 4 2015/2016 2nd attempt 11 5187 Juli Kan 8
Subject 7 2015/2016 Complaint 0 5187 Juli Kan 14
Subject 5 2015/2016 1st attempt 16 5187 Juli Kan 10
Subject 7 2015/2016 2nd attempt 15 5187 Juli Kan 14
Subject 7 2015/2016 1st attempt 4 5187 Juli Kan 14
Subject 1 2015/2016 1st attempt 5 5187 Juli Kan 2
Subject 1 2015/2016 2nd attempt 6 5187 Juli Kan 2
Subject 1 2015/2016 Complaint 0 5187 Juli Kan 2
Subject 5 2015/2016 Complaint 0 5187 Juli Kan 10
Subject 5 2015/2016 Complaint 0 5187 Juli Kan 10
Subject 1 2015/2016 Complaint 11 5187 Juli Kan 2
Subject 8 2015/2016 1st attempt 8 5187 Juli Kan 16
Subject 9 2015/2016 Complaint 0 5187 Juli Kan 18
Subject 9 2015/2016 1st attempt 12 5187 Juli Kan 18
Subject 8 2015/2016 Complaint 11 5187 Juli Kan 16
Subject 8 2015/2016 2nd attempt 11 5187 Juli Kan 16
Subject 8 2015/2016 Complaint 0 5187 Juli Kan 16
Subject 6 2015/2016 2nd attempt 10 5187 Juli Kan 12
Subject 6 2015/2016 1st attempt 9 5187 Juli Kan 12
Subject 3 2015/2016 equivalent 0 5187 Juli Kan 6
Subject 3 2015/2016 2nd attempt 2 5187 Juli Kan 6
Subject 3 2015/2016 equivalent 0 5187 Juli Kan 6
Subject 3 2015/2016 1st attempt 17 5187 Juli Kan 6
Subject 2 2015/2016 1st attempt 3 5187 Juli Kan 4
Subject 2 2015/2016 Complaint 12 5187 Juli Kan 4
Subject 10 2015/2016 2nd attempt 14 5187 Juli Kan 20
Subject 10 2015/2016 1st attempt 3 5187 Juli Kan 20