1

2つのテーブルがあります:

クラス(CourseNo、SectionNo、Year、Semester、Capacity)(太字はキーを示します)。

登録(CourseNo、Year、Semester、SectionNo、StudentNo、Grade)

各クラスの登録数がそのクラスの容量よりも少なくなるようにするには、SQLをどのように使用しますか?

パートを個別に行う方法は知っていますが、各クラスに登録されている生徒のcount()とそのクラスの定員を比較すると、迷子になります。正しい値と比較する方法がわかりません。

ありがとう

編集:私はSQL Server 2008を使用してこのアサーションを作成していますが、SQL Serverでトリガーを作成することに加えて、通常のSQLを作成する方法を知りたいです(アサーションをサポートしていないため)

4

2 に答える 2

0
select courseno, capacity, count(*) as enrollments
from classes c 
join enrollments e on e.courseno = c.courseno
group by c.courseno , capacity
于 2012-11-15T03:03:37.457 に答える
0
select c.CourseNo, c.SectionNo, c.[Year], c.Semester, Capacity, count(*) as Enrolled
from classes as c 
 join enrollments as e
  on c.CourseNo = e.CourseNo
   and c.SectionNo = e.SectionNo
   and c.[Year] = e.[Year]
   and c.Semester = e.Semester
 group by c.CourseNo, c.SectionNo, c.[Year], c.Semester, Capacity
 having count(StudentNo) > Capacity
于 2012-11-15T09:05:27.753 に答える