简体   繁体   English

计算查询中的GPA

[英]Calculating GPA in query

I'm having some trouble connecting the tables on this one to get a single result. 我在连接此表上的某个表时遇到了一些麻烦。 Here's how you calculate GPA: 计算GPA的方法如下:

  1. Get the average grade for each course. 获得每门课程的平均成绩。
  2. Multiply each average by the number of credit hours in said course. 将每个平均值乘以该课程中的学时数。
  3. Add the results from 2. 从2中添加结果。
  4. Divide the result from 3 by the total number of credit hours. 将结果从3除以总学时。

I have three relevant tables: 我有三个相关的表:

CREATE TABLE Student
(
SSN        INT(11),
SName    VARCHAR(25),
Major    VARCHAR(25),
PRIMARY KEY (SSN)
);

CREATE TABLE Course
(
CNO        INT(11),
CName    VARCHAR(3),
CreditHour    TINYINT(4),
PRIMARY KEY (CNO)
);

CREATE TABLE Grade
(
SSN        INT(11),
CNO        INT(11),
Grade        TINYINT(4)
);

Unfortunately, SQL fiddle seems to be down, so I can't make a fiddle for this. 不幸的是,SQL提琴似乎已经倒闭了,所以我不能为此提个琴。 How would I make a select statement to list the student info with a calculated gpa column? 我如何做出选择语句以列出带有计算出的gpa列的学生信息? Every time I attempt this, I end up with an overblown set of nested subqueries, like this: 每次尝试进行此操作时,都会出现一组过分的嵌套子查询,如下所示:

SELECT student.*, (total/hours) as gpa WHERE hours = 
   (SELECT SUM(course.creditHours) AS hours 
   FROM course, student, grade 
   WHERE course.cno = grade.cno 
   AND grade.ssn = student.ssn 
   GROUP BY course.cno) 
   AND total = (SELECT SUM(AVG(grade.grade)*course.creditHours) as total 
   FROM grade, course, student 
   WHERE grade.ssn = student.ssn 
   AND grade.cno = course.cno)

That doesn't actually appear to be going in the right direction, either. 实际上,这似乎也不是朝着正确的方向发展。 What should I be doing to derive this column? 我应该怎么做才能得出本专栏?

If you want the GPA by Student, I would calculate as follows: 如果您希望按学生获得GPA,我将进行以下计算:

1 Multiply the number of credit hours for each course by the Grade and add up the product, per Student 1将每门课程的学时数乘以成绩,然后将每个学生的乘积相加

2 Divide the result from 1 by the total number of credit hours, per student. 2将结果从1除以每个学生的学时总数。

SELECT
    s.SSN,
    SUM(c.CreditHour * g.Grade) / SUM(c.CreditHour) Average_GPA
FROM Student s
INNER JOIN Grade g on s.SSN = g.SSN
INNER JOIN Course c on g.CNO = c.CNO
GROUP BY s.SSN;

If you simply want the weighted average GPA for each course, then the following query can be used using the steps from your question: 如果您只希望每门课程的加权平均GPA,则可以使用问题中的步骤使用以下查询:

1.Get the average grade for each course. 1.获取每门课程的平均成绩。

2.Multiply each average by the number of credit hours in said course. 2.将每个平均数乘以该课程的学时数。

3.Add the results from 2. 3.将2中的结果相加。

4.Divide the result from 3 by the total number of credit hours. 4.将结果从3除以总学时数。

SELECT
    c.CNO,
    SUM(c.CreditHour * average_gpa.GPA) / SUM(c.CreditHour) Weighted_Average_GPA
FROM Course c
INNER JOIN
(
SELECT
    CNO,
    AVG(g.Grade) GPA
FROM Grade
GROUP BY CNO
) average_gpa
ON c.CNO = average_gpa.CNO
GROUP BY c.CNO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM