简体   繁体   English

总和然后按SQL数据库中的查询分组

[英]Sum then divide with group by query in SQL database

Recently I am facing problem with the SUM() function and GROUP BY clause of SQL. 最近,我SUM()SUM()函数和SQL的GROUP BY子句的问题。 I want to find the GPA (Grade Point Average) on the basis of float values. 我想根据浮动值找到GPA(平均绩点)。

The following is the code that I have been using so far: 以下是到目前为止我一直在使用的代码:

SELECT semesterid,
       SUM( value ) AS TotalValue
FROM tbcourse
GROUP BY semesterid;

If value is the individual grade points, then the following will give you the GPA (Grade Point Average) for each value of semesterid . 如果value是各个成绩点,则以下内容将为您提供每个semesterid值的GPA(平均成绩点)。

SELECT semesterid,
       AVG( value ) AS TotalValue
FROM tbcourse
GROUP BY semesterid;

If you have any questions or comments, then please feel free to post a Comment accordingly. 如果您有任何问题或意见,请随时发表评论。

You're on the right track using a GROUP BY with the SUM() aggregate function. 使用GROUP BY和SUM()聚合函数可以使您处于正确的轨道。 Part of the trick here, though, is that you need to grab data (products of hours*points) for each class, which you get from an aggregate. 但是,这里技巧的一部分是您需要获取每个类的数据(小时*点的乘积),这些数据是从聚合中获取的。 So you need two "query steps", one where you aggregate the INPUT data for the division and then the step for calculating "points / credit hours". 因此,您需要两个“查询步骤”,一个步骤是汇总除法的INPUT数据,然后是计算“点/学分”的步骤。

My suggestion for fixing this sort of issue is to use an INNER QUERY. 我对解决此类问题的建议是使用内部查询。 Basically, you use FROM(...) to write a second "inner" query to make a specific or custom set of data to SELECT from. 基本上,您使用FROM(...)编写第二个“内部”查询,以生成一组特定的或自定义的数据以供SELECT选择。

So what does that mean for your problem? 那么,这对您的问题意味着什么呢?

I wasn't sure what your data actually looks like, so I took a stab and assumed two tables. 我不确定您的数据实际上是什么样子,所以我尝试了一下并假设有两个表。 Since you have a semester ID and courses (which also have an ID I assume), I took that to mean you had at least those. 由于您有一个学期的ID和课程(我也假设有ID),因此我认为这意味着您至少拥有这些ID。 I assumed some simple table structures and here's what I would do in your case following my advice: 我假设了一些简单的表结构,这是在我的建议下针对您的情况所做的工作:

Semesters Table 学期表

CREATE TABLE semesters (
  id INT(12) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  term_name VARCHAR(256) NOT NULL,
  term_year INT(8) NOT NULL
 );

Courses Table 课程表

CREATE TABLE courses (
  id INT(12) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(256) NOT NULL,
  semester_id INT(12) NOT NULL,
  credit_hours INT(4) NOT NULL,
  grade FLOAT,
  FOREIGN KEY (semester_id) REFERENCES semesters(id)
 );

Query 1 :: GROUP BY each semester to see GPA by semester 查询1 ::每个学期的GROUP BY以查看每个学期的GPA

SELECT
  sg.semester_id AS "Semester"
  ,sg.grade_points AS GPs
  ,sg.hours AS hours
  ,ROUND((sg.grade_points / sg.hours), 2) AS GPA
FROM (
  SELECT
    c.semester_id AS semester_id
    ,SUM(c.credit_hours + 0.0) AS hours
    ,SUM((c.grade * c.credit_hours) + 0.0) AS grade_points
  FROM courses AS c
  GROUP BY c.semester_id
) as sg  # sg ~ SemesterGrades;

Query 2 :: # Query 2 - SUM over ALL semesters to get (weighted) cumulative GPA 查询2 ::#查询2-所有学期的总和以获得(加权)累积GPA

SELECT
  ROUND((grades.grade_points / grades.hours), 2) AS GPA
FROM (
  SELECT
    c.semester_id AS semester_id
    ,SUM(c.credit_hours + 0.0) AS hours
    ,SUM((c.grade * c.credit_hours) + 0.0) AS grade_points
  FROM courses AS c
) as grades

I also made up an SQL-Fiddle so you can play around with this and, if you want, change the table structure and try out more queries to get it right. 我还编写了一个SQL-Fiddle,以便您可以解决此问题,并且,如果需要,可以更改表结构并尝试进行更多查询以使其正确。 Also, feel free to ask for more help if I didn't answer your question! 另外,如果我没有回答您的问题,请随时寻求更多帮助! Hope it helps. 希望能帮助到你。

http://sqlfiddle.com/#!9/2b0693 http://sqlfiddle.com/#!9/2b0693

UPDATE I don't believe the use of AVG() is correct for this problem. 更新我不认为使用AVG()解决此问题是正确的。 The reason is that GPA calculations are not a "standard" average - it's a weighted average. 原因是GPA计算值不是“标准”平均值-而是加权平均值。 Said in other words, the AVG() is a special case of a weighted average where the weight on all the elements (credits) is the same (it's not for classes with different numbers of credit hours. 换句话说,AVG()是加权平均值的特例 ,其中所有元素(学分)的权重都相同(不适用于学分小时数不同的班级)。

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

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