1

私は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
                );

もっと良い方法はありますか?どうもありがとう!

4

1 に答える 1

0

How about this?

select stuid.dno, count (*)
from stuid inner join student
on stuid.stuid = student.stuid 
where student.major in (550, 600)
group by stuid.dno
order by count (*) desc

I am assuming that dnos 550 and 600 are the computer science courses

于 2013-10-15T03:19:21.357 に答える