Hi I am new to Oracle SQL and i want to write an SQL statement that can read the grades from the table and generate gpa for students. Below is a sample code I wrote and its not working:
select id,
declare gpa = 0;
BEGIN
IF grade = 'A+' THEN gpa = gpa + 4.5;
ELSIF grade = 'A' THEN gpa = gpa + 4;
ELSIF grade = 'B+' THEN gpa = gpa + 3.5;
ELSIF grade = 'B' THEN gpa = gpa + 3;
ELSIF grade = 'C+' THEN gpa = gpa + 2.5;
ELSIF grade = 'C' THEN gpa = gpa + 2;
ELSE gpa = gpa + 0;
END IF
gpa = gpa/count(grade)
END
gpa from table where id in ('s11','s12','s13','s14')
group by id
The example of data in the table is as such:
id grade
--------------------------
s11 A
s11 B+
s11 A+
s11 C
s12 C+
s12 A
s12 D
s12 B
s13 A+
s13 C+
s13 B
s13 A
s13 A
....
....
....
....
Can anyone please help????
Try something like this :