SQL Server:创建前三视图-遇到困难

时间:2018-12-02 17:26:46

标签: sql sql-server

这是我第一次问与SQL有关的问题。我很难让此查询正常工作。

基本上,我有一个包含3个表CourseFacultyAdjunct的数据库。该查询应该创建一个名为Top3Enrollment的视图,该视图返回FirstName, LastName, TotalStudentsMaxEnrollment的3名教职员工中其课程总注册人数最多,并且课程中的最高注册人数他们教。

尝试编写查询时,选择列FirstName

时出现错误

我的查询:

CREATE VIEW Top3Enrollment
AS
    SELECT TOP 3 PERCENT 
        FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
    FROM 
        Faculty 
    JOIN 
        Course ON Faculty.Faculty_ID = Course.Faculty_ID
    ORDER BY 
        MAX(Enrollment);

我得到的错误是:

  

消息8120,级别16,状态1,过程Top3Enrollment,第3行[批处理开始第0行]
  选择列表中的“ Faculty.FirstName”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

我的数据库结构:

CREATE TABLE Faculty
(
    Faculty_ID VARCHAR(2), 
    LastName VARCHAR(20), 
    FirstName VARCHAR(20), 
    Department VARCHAR(10), 
    Campus VARCHAR(10)
); 

INSERT INTO Faculty 
VALUES ('1', 'Brown', 'Joe', 'Business', 'Kent'),
       ('2', 'Smith', 'John', 'Economics', 'Kent'),
       ('3', 'Jones', 'Sally', 'English', 'South'),
       ('4', 'Black', 'Bill', 'Economics', 'Deerwood'),
       ('5', 'Green', 'Gene', 'Business', 'South'); 

CREATE TABLE Course
(
    Course_ID CHAR(2), 
    Ref_Number CHAR(5),
    Faculty_ID VARCHAR(2),
    Term Char(1),
    BegDate SMALLDATETIME,
    Enrollment INTEGER,
    TotRev FLOAT 
); 

INSERT INTO Course 
VALUES ('1', '12345', 'a', 'A', '2016-01-08 00:00:00', 24, 12345.32 ),
       ('2', '54321', '3', 'B', '2016-02-04 00:00:00', 18, 21435.51 ),
       ('3', '13524', '1', 'B', '2016-02-04 00:00:00', 7,  1256.67 ),
       ('4', '24653', '1', 'C', '2016-03-04 00:00:00', 29, 54421.11 ),
       ('5', '98765', '5', 'A', '2016-01-08 00:00:00', 35, 246753.23),
       ('6', '14862', '2', 'B', '2016-02-04 00:00:00', 14, 9876.87),
       ('7', '96032', '1', 'C', '2016-03-04 00:00:00', 8,  863159.31),
       ('8', '81256', '5', 'A', '2016-01-08 00:00:00', 5,  98762.14),
       ('9', '64321', '2', 'C', '2016-03-04 00:00:00', 23, 2965.98),
       ('10','90908', 'a', 'A', '2016-01-08 00:00:00', 45, 91724.02),
       ('11','90908', '3', 'A', '2016-01-08 00:00:00', 23, 73725.77),
       ('12','90908', '3', 'A', '2016-01-08 00:00:00', 16, 84224.29),
       ('13','90908', 'b', 'A', '2016-01-08 00:00:00', 13, 42719.82);

CREATE TABLE Adjuncts
(
    Faculty_ID Char(2), 
    LastName VARCHAR(20), 
    FirstName VARCHAR(20), 
    Department VARCHAR(10), 
    Campus VARCHAR(10)
); 

INSERT INTO Adjuncts 
VALUES ('a', 'Rogers', 'Aaron', 'Business', 'Kent'),
       ('b', 'Manning', 'Peyton', 'Economics', 'North'),
       ('c', 'Drew', 'Maurice', 'English', 'Cecil'),
       ('d', 'Griffin', 'Robert', 'Music', 'Deerwood'),
       ('e', 'Goodell', 'Roger', 'Economics', 'South'),
       ('f', 'Vilma', 'Jonathan', 'Business', 'Kent');  

注意:

我了解我无法拥有Order By,但我不知道还能使用什么

添加了数据库代码

1 个答案:

答案 0 :(得分:0)

当将sum和max之类的聚合函数与其他列一起使用时,需要按其他列对聚合进行分组。

像这样添加GROUP BY

SELECT TOP 3 PERCENT FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
FROM Faculty 
JOIN Course ON Faculty.Faculty_ID = Course.Faculty_ID
GROUP BY FirstName, LastName
ORDER BY MAX(Enrollment);