我有一张表格,其中包含以下信息:ID,姓名,course_num,积分,成绩以及我需要计算每位学生的GPA。查询应该很简单,但是成绩存储为字母字符,所以我必须先转换它才能进行任何添加。如何在不修改表格中的值的情况下执行此操作?
A:4.0; A-:3.67; B +:3.33; B:3; B-:2.67; C +:2.33; C:2; C-:1.67; D +:1.33; D:1; D-:0.67; F:0
我应该指出,我对该信息的表是子查询的结果,该子查询连接3个表,分别称为student,take和course,分别存储学生信息,成绩和课程信息。
最终将使用JDBC
将其置于java方法中以下是我尝试的声明,我知道该声明已关闭,但我觉得它已走上正轨?
SELECT ID, name, (sum( case grade
WHEN 'A' THEN 4
WHEN 'A-' THEN 3.67
WHEN 'B+' THEN 3.33
WHEN 'B' THEN 3
WHEN 'B-' THEN 2.67
WHEN 'C+' THEN 2.33
WHEN 'C' THEN 2
WHEN 'C-' THEN 1.67
WHEN 'D+' THEN 1.33
WHEN 'D' THEN 1
WHEN 'D-' THEN .67
WHEN 'F' THEN 0)*credits)/sum(credits)) GPA
FROM (SELECT takes.ID, name, cid, grade, credits
FROM student Natural Join takes Natural Join course) subQ
GROUP BY ID;
我也有这个解决方案,但我不知道我在哪里可以插入gpa#
SELECT takes.id, name, sum(credits * gpa#) / sum(credits) AS GPA
FROM takes NATURAL JOIN student NATURAL JOIN course
GROUP BY takes.ID
答案 0 :(得分:0)
您的查询正确。我刚刚将name
添加到group by
子句中,修改了数学并将其格式化了一点:
SELECT
ID,
`name`,
sum(
case grade
WHEN 'A' THEN 4
WHEN 'A-' THEN 3.67
WHEN 'B+' THEN 3.33
WHEN 'B' THEN 3
WHEN 'B-' THEN 2.67
WHEN 'C+' THEN 2.33
WHEN 'C' THEN 2
WHEN 'C-' THEN 1.67
WHEN 'D+' THEN 1.33
WHEN 'D' THEN 1
WHEN 'D-' THEN .67
WHEN 'F' THEN 0
ELSE 0
end * credits
) / sum(credits) as GPA
FROM
(
SELECT takes.ID, name, cid, grade, credits
FROM student
inner join takes
inner join course
) subQ
GROUP BY
ID,
`name`;
试一试,看看它是如何为你做的。