複数のテーブルの情報から学生の GPA を計算するクエリを作成しました。
SELECT Major DNO, DName Dept, FName, LName,
(SUM(g.gradepoint*c.Credits)/SUM(c.Credits)) GPA
FROM Student s, Enrolled_in e, Gradeconversion g, Course c, Department d
WHERE s.StuID=e.StuID and e.Grade=g.lettergrade and e.CID=c.CID and d.DNO=s.Major
GROUP BY e.StuID;
次の出力が生成されます。
+------+-----------------------+---------+----------+------------------+
| DNO | Dept | FName | LName | GPA |
+------+-----------------------+---------+----------+------------------+
| 600 | Computer Science | Linda | Smith | 3.3187500089407 |
| 600 | Computer Science | Tracy | Kim | 3.08235291873708 |
| 600 | Computer Science | Shiela | Jones | 3.05999999046326 |
| 600 | Computer Science | Dinesh | Kumar | 2.78947370930722 |
| 600 | Computer Science | Paul | Gompers | 2.89999998699535 |
| 600 | Computer Science | Andy | Schultz | 2.98823530533734 |
| 600 | Computer Science | Lisa | Apap | 3.32105263910796 |
| 600 | Computer Science | Jandy | Nelson | 3.2 |
| 600 | Computer Science | Eric | Tai | 3.01923077840071 |
| 600 | Computer Science | Derek | Lee | 3.61304346374843 |
| 600 | Computer Science | David | Adams | 3.3 |
| 600 | Computer Science | Steven | Davis | 3.18750002980232 |
| 600 | Computer Science | Charles | Norris | 3.57142857142857 |
| 600 | Computer Science | Susan | Lee | 3.5071428673608 |
| 600 | Computer Science | Mark | Schwartz | 2.9434782733088 |
| 600 | Computer Science | Bruce | Wilson | 3.05789474437111 |
| 600 | Computer Science | Michael | Leighton | 3.17058825492859 |
| 600 | Computer Science | Arthur | Pang | 2.89999998699535 |
| 520 | ECE | Ian | Thornton | 4 |
| 520 | ECE | George | Andreou | 2.94782609524934 |
| 540 | Chemical Engineering | Michael | Woods | 3.26666665960241 |
| 520 | ECE | David | Shieber | 3.375 |
| 540 | Chemical Engineering | Stacy | Prater | 3.06666667373092 |
| 520 | ECE | Mark | Goldman | 3.42307692307692 |
| 520 | ECE | Eric | Pang | 3.7562500089407 |
| 520 | ECE | Paul | Brody | 2.90526317295275 |
| 550 | Mathematical Sciences | Eric | Rugh | 3.82499998807907 |
| 100 | History | Jun | Han | 3.10500003099442 |
| 550 | Mathematical Sciences | Lisa | Cheng | 2.95384616118211 |
| 550 | Mathematical Sciences | Sarah | Smith | 3.09230767763578 |
| 550 | Mathematical Sciences | Eric | Brown | 2.98000001907349 |
| 550 | Mathematical Sciences | William | Simms | 3.8 |
| 50 | Cognitive Science | Eric | Epp | 3.11249998211861 |
| 50 | Cognitive Science | Sarah | Schmidt | 3.08125002682209 |
+------+-----------------------+---------+----------+------------------+
ここで、各専攻で最低の GPA を持つ学生に対応するこのテーブルのタプルを見つける必要があります。私の考えは、次のようなことをすることでした:
SELECT DNO, Dept, FName, LName, MIN(GPA) GPA FROM
(SELECT Major DNO, DName Dept, FName,
LName,(SUM(g.gradepoint*c.Credits)/SUM(c.Credits)) GPA
FROM Student s, Enrolled_in e, Gradeconversion g, Course c, Department d
WHERE s.StuID=e.StuID and e.Grade=g.lettergrade and e.CID=c.CID and d.DNO=s.Major
GROUP BY e.StuID) p
GROUP BY Dept;
しかし、それは生成します:
+------+-----------------------+---------+----------+------------------+
| DNO | Dept | FName | LName | GPA |
+------+-----------------------+---------+----------+------------------+
| 540 | Chemical Engineering | Michael | Woods | 3.06666667373092 |
| 50 | Cognitive Science | Eric | Epp | 3.08125002682209 |
| 600 | Computer Science | Linda | Smith | 2.78947370930722 |
| 520 | ECE | Ian | Thornton | 2.90526317295275 |
| 100 | History | Jun | Han | 3.10500003099442 |
| 550 | Mathematical Sciences | Eric | Rugh | 2.95384616118211 |
+------+-----------------------+---------+----------+------------------+
各学科の最小 GPA が検出されますが、実際に GPA が最も低かった fname と lname ではなく、たまたまその学科で最初にリストされた学生の fname と lname に関連付けられます。GROUP BY を使用する場合、集計関数の一部ではない SELECT 内のすべての属性が GROUP BY に表示される必要があることはわかっています。探している値を取得するためにこのクエリを続行する方法がわかりません。私はこれがまったく新しいので、どんな助けでも大歓迎です!