我有一个任务,我有点挂在 SQL Server 上,想知道是否有人可以提供帮助。
任务:
在数据库中,我需要创建一个名为 的存储过程ClassRegistration
。此存储过程的结果集的列需要如下所示:
[Class]
– 要教授的班级名称[Teacher Name]
- 授课人的姓名[Registrations]
- 已注册上课的学生人数[Number Paid]
- 已支付课程费用的学生人数
表:
[Teacher]
:Teacher_ID
(PK),TeacherName
[Class]
:Class_ID
(PK),ClassName
,Teacher_ID
(FK)[ClassRegistration]
Student_ID
(FK),Class_ID
(FK),HasPaidFees
[Student]
Student_ID
(PK),StudentName
即使没有人注册,学院也需要了解每一门课程。
到目前为止我的 SQL:
Create PROCEDURE ClassRegistration
AS
SELECT DISTINCT
c.ClassName,
t.TeacherName,
COUNT(cr.Student_ID) As Registrations,
COUNT(case when cr.HasPaidFees = 1 then 1 else null end) As NumberPaid
FROM
Class As c,
Teacher As t,
ClassRegistration As cr,
Student As s
WHERE
(c.Class_ID = cr.Class_ID)
AND (cr.Student_ID = s.Student_ID)
GROUP BY c.ClassName, t.TeacherName
我可以很好地创建存储过程,但输出看起来不正确,任何帮助将不胜感激:)。