我已经在 SQL 中创建了一些表,它们如下:
CREATE TABLE Courses(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
)
CREATE TABLE CourseSections(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
SectionNo INTEGER,
InstructorNo INTEGER NOT NULL,
Year INTEGER,
Semester INTEGER,
RoomNo INTEGER NOT NULL,
Weekday INTEGER NOT NULL,
StartTime INTEGER NOT NULL,
FinishTime INTEGER NOT NULL,
Capacity INTEGER NOT NULL,
PRIMARY KEY(CourseNo, SectionNo, Year, Semester)
)
CREATE TABLE Instructor(
InstructorNo INTEGER PRIMARY KEY,
FirstName VARCHAR(40) NOT NULL,
LastName VARCHAR(40) NOT NULL
)
CREATE TABLE Students(
StudentNo INTEGER PRIMARY KEY,
FirstName VARCHAR(40) NOT NULL,
LastName VARCHAR(40) NOT NULL,
Year INTEGER,
GPA REAL
)
CREATE TABLE Enrollments(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
Year INTEGER,
Semester INTEGER,
SectionNo INTEGER,
StudentNo INTEGER,
Grade REAL,
PRIMARY KEY(CourseNo, Year, Semester, SectionNo, StudentNo)
)
CREATE TABLE Areas(
AreaName VARCHAR(40) PRIMARY KEY
)
CREATE TABLE AreasOfCourse(
CourseNo INTEGER CHECK(CourseNo>=100 AND CourseNo<=999),
AreaName VARCHAR(40),
PRIMARY KEY(CourseNo, AreaName)
)
CREATE TABLE AreasOfInstructor(
InstructorNo INTEGER,
AreaName VARCHAR(40),
PRIMARY KEY(InstructorNo, AreaName)
)
现在,我想找到每门课程的标题,以及每学期该课程的总入学人数,我必须为此写一个查询。所以我的工作如下:
SELECT c1.Title, COUNT(e1.SectionNo), e1.Semester
FROM Courses c1, Courses c2, Enrollments e1, Enrollments e2
WHERE c1.CourseNo = c2.CourseNo AND e1.Semester = e2.Semester AND
e1.SectionNo <> e2.SectionNo
执行此查询时出现错误。错误是:
选择列表中的“Courses.Title”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
谁能帮我弄清楚如何解决这个问题?