嗨,我是 Oracle SQL 的新手,我想编写一个 SQL 语句,可以从表中读取成绩并为学生生成 gpa。以下是我编写的示例代码,但它不起作用:
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
表中数据示例如下:
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
....
....
....
....
有人可以帮忙吗????