1

我已经在 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 子句中。

谁能帮我弄清楚如何解决这个问题?

4

5 回答 5

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

http://sqlfiddle.com/#!6/efa4b/1

于 2012-10-22T19:20:04.480 回答
1

函数 COUNT() 计算元组/行的数量并显示结果。如果您想计算结果中的所有行,那将为您提供单值结果,并且与您的其余结果不兼容。它会将单个结果放在哪里,对每一行重复它?

因此,您需要添加一个 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 组合。

于 2012-10-22T19:25:56.953 回答
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
于 2012-10-22T19:18:23.187 回答
0

好吧,您需要按c1.Titleand分组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
于 2012-10-22T19:19:33.530 回答
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在查询中添加 a),您将获得数,而不是不同 SectionNo 的总数。

如果您想按 Title、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;
于 2012-10-22T19:20:46.397 回答