我已经在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子句中。
有人可以帮我弄清楚如何解决这个问题吗?
答案 0 :(得分:1)
如果我说得对,你的查询有点过于复杂
select
C.Title,
E.Semester,
count(*) as Enrollment
from Courses as C
left outer join Enrollments as E on E.CourseNo = C.CourseNo
group by
C.Title,
E.Semester
答案 1 :(得分:1)
函数COUNT()计算tupels / rows的数量并显示结果。如果要计算结果中的所有行,则会产生单个值结果,并且与结果的其余部分不兼容。它会把单个结果放在哪里,为每一行重复一次?
因此,您需要添加GROUP BY子句。通过这样做,您可以将多个元组组合在一起,然后每个组可以对它们进行计数,并显示每个组计数的单个结果。
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
GROUP BY c1.Title, e1.Semester
正如错误消息所说,count是一个聚合函数,只显示1个结果,不能与具有多个元组的SELECT结合使用。
答案 2 :(得分:0)
错误消息实际上很简单:您缺少GROUP BY
子句
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
GROUP BY c1.Title, e1.Semester
答案 3 :(得分:0)
您需要按c1.Title
和e1.Semester
分组:
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
GROUP BY c1.Title, e1.Semester
答案 4 :(得分:0)
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
无法保证c1.Title与不同的e1.SectionNo保持一致,因此SQL不知道该回答什么。
您必须按标题对结果进行分组(在查询中添加GROUP BY c1.Title
),您将获得行的计数,而不是不同的SectionNo的总数。
如果你想按标题,SectionNo和Semester获得总数,你必须GROUP BY所有三列:
SELECT c1.Title, e1.SectionNo, e1.Semester, COUNT(*) AS total
FROM Courses c1,
Courses c2,
Enrollments e1,
Enrollments e2
WHERE c1.CourseNo = c2.CourseNo
AND e1.Semester = e2.Semester
AND e1.SectionNo <> e2.SectionNo
GROUP BY c1.Title, e1.SectioNo, e1.Semester;
或者,如果你想要按标题和学期划分的部分,
SELECT c1.Title, e1.Semester, COUNT(e1.SectionNo) AS total
FROM Courses c1,
Courses c2,
Enrollments e1,
Enrollments e2
WHERE c1.CourseNo = c2.CourseNo
AND e1.Semester = e2.Semester
AND e1.SectionNo <> e2.SectionNo
GROUP BY c1.Title, e1.Semester;