私はSQLが初めてなので、質問はばかげているかもしれません。
関連テーブルのサンプル行を次に示します。 Student:
+-------+----------+---------+------+------+-------+---------+-----------+
| StuID | LName | Fname | Age | Sex | Major | Advisor | city_code |
+-------+----------+---------+------+------+-------+---------+-----------+
| 1001 | Smith | Linda | 18 | F | 600 | 1121 | BAL |
マイナーイン:
+-------+------+
| StuID | DNO |
+-------+------+
| 1004 | 520 |
部門:
+------+----------+--------------------------+------+--------------+--------+
| DNO | Division | DName | Room | Building | DPhone |
+------+----------+--------------------------+------+--------------+--------+
| 10 | AS | History of Art | 268 | Mergenthaler | 7117 |
CS メジャー (DNO = 600) で最も人気のある未成年の学生を見つけたいです。
SELECT DNO, count
FROM (SELECT DNO, COUNT(*) AS count
FROM Minor_in
WHERE StuID IN (SELECT StuID
FROM Student
WHERE (Major = 550 OR Major = 600))
GROUP BY DNO
) AS C1
WHERE count IN (
SELECT MAX(count)
FROM (
SELECT DNO, COUNT(*) AS count
FROM Minor_in
WHERE StuID IN (SELECT StuID
FROM Student
WHERE (Major = 550 OR Major = 600))
GROUP BY DNO
) AS MinorCount
);
もっと良い方法はありますか?どうもありがとう!