2012-10-22 109 views
1

我已經創建了SQL的某些表已經,它們低於:如何在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子句是不 。

任何人都可以幫我弄清楚如何解決這個問題嗎?

回答

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 

作爲該錯誤消息表示,計數的聚集函數,僅顯示1個結果,不能以選擇具有多個元組進行組合。

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 
0

那麼你需要按c1.Titlee1.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 
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

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和學期總,你必須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;