简体   繁体   English

在SQL查询中使用Count和Max

[英]Using Count and Max in SQL Query

I have two tables that I am trying to query from, Enrollment and Course. 我有两个表格要查询,分别是“注册”和“课程”。 In the course Table, there is just one entry for each course, but in the Enrollment table, there is an entry for each student that is enrolled in any course, so there may be 30 entries for one course. 在“课程表”中,每个课程只有一个条目,但是在“注册”表中,每个课程的每个学生都有一个条目,因此一个课程可能有30个条目。 My task is to find The course that has the most enrollments, and print out that course name, as well as the number of enrollments for that course. 我的任务是找到注册人数最多的课程,并打印出该课程的名称以及该课程的注册人数。 Here is my query so far 到目前为止,这是我的查询

select c.CourseCode ,(SELECT count( * ) FROM Enrollment WHERE CourseCode = c.CourseCode) as test from Course c ;

this gives me the results: 这给了我结果:

CS227 - 29 CS227-29

CS228 - 34 CS228-34

CS309 - 31 第309章-31

CS311 - 25 , ect, which is good, but NOW, how do I print out only the class that has the most enrollments(in this case, CS228). CS311-25等,这很好,但是现在,我如何只打印出注册人数最多的班级(在本例中为CS228)。 I have tried using the max(), but I can't get anything to work. 我已经尝试过使用max(),但是我什么都无法工作。

Here is the table structure 这是表结构

create table Course( CourseCode char(50), CourseName char(50), PreReq char (6)); 创建表Course(CourseCode char(50),CourseName char(50),PreReq char(6));

create table Enrollment ( CourseCode char(6) NOT NULL, SectionNo int NOT NULL, StudentID char(9) NOT NULL references Student, Grade char(4) NOT NULL, primary key (CourseCode,StudentID), foreign key (CourseCode, SectionNo) references Offering); 创建表注册(CourseCode char(6)NOT NULL,SectionNo int NOT NULL,StudentID char(9)NOT NULL引用Student,Grade char(4)NOT NULL,主键(CourseCode,StudentID),外键(CourseCode,SectionNo)参考产品);

Just take the top 1 after ordering by the count. 只需按计数排序即可获得前1名。

That is: 那是:

Select Top 1 A.CourseCode, Count(*) From Course A inner join Enrollment B on (A.CourseCode=B.CourseCode) 
Group By A.CourseCode
Order By Count(*) DESC

Also - use an inner join as I've shown here rather than a subquery. 另外-使用这里显示的内部联接而不是子查询。 I do tend to like SubQueries and this one will work but it is just not appropriate in this kind of query! 我确实喜欢SubQueries,并且这个查询可以工作,但是在这种查询中不合适!

Based on your comment, I think the blow query is what you want, although it is untested and I am not entirely sure on the HAVING clause being valid. 根据您的评论,我认为打击查询是您想要的,尽管未经测试,并且我不确定HAVING子句是否有效。 From the documentation on MySQL's page, it seems it should work. 从MySQL页面上的文档看来,它应该可以工作。

SELECT A.CourseCode, COUNT(*) AS count FROM Course A
JOIN Enrollment B ON A.CourseCode = B.CourseCode
GROUP BY A.CourseCode
HAVING count = MAX(count)

As for performance, I cannot tell if it's a good idea to run a MAX on an aggregate function (probably not). 至于性能,我无法判断在聚合函数上运行MAX是否是一个好主意(可能不是)。

Otherwise, just use the other query to return the top X and simply run through comparing to the previous number. 否则,只需使用其他查询返回前X个,然后简单地与前一个数字进行比较即可。

SELECT 
      c.CourseCode, 
      c.CourseName,
      COUNT(*) AS cnt 
FROM
      Course AS c
  INNER JOIN
      Enrollment AS e
          ON c.CourseCode = e.CourseCode 
GROUP BY
      c.CourseCode
HAVING 
      COUNT(*) = 
         ( SELECT  
                 COUNT(*) AS cnt 
           FROM
                 Enrollment AS e
           GROUP BY
                 e.CourseCode
           ORDER BY
                 cnt DESC
           LIMIT 1
         )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM