0

我遇到了 SQL 子查询的问题:我需要编写一个查询,该查询返回订阅数 (count(employeeNumber)) 大于最大允许订阅数 (Maximum) 的课程。

在我的原始查询中,我收到以下错误: Group function is not allowed here

查询:

SELECT c.CourseName 
FROM courses c 
INNER JOIN subscriptions s ON s.courseCode = c.CourseCode 
INNER JOIN plannedCourses p ON p.CourseCode = s.CourseCode
WHERE COUNT(s.EmployeeNumber) > (SELECT maximum 
                                 FROM plannedCourses 
                                 WHERE s.CourseCode = p.CourseCode);

表布局:

表格布局

我怎样才能达到期望的结果?

提前致谢!!

4

2 回答 2

1

您可以按如下方式重写您的查询:

select c.coursename
  from courses c
  join subscriptions s
    on (s.coursecode = c.coursecode)
  join PlannedCourses p
    on (p.coursecode = c.coursecode)
  group by c.coursename 
         , p.maximum
  having count(s.Employeenumber) > p.maximum 
于 2013-07-25T13:42:56.710 回答
0

您的查询有多个问题。您正在使用相关子查询,但您没有在其from子句中使用该表。我认为意图是这样的:

SELECT c.CourseName 
FROM courses c 
INNER JOIN subscriptions s ON s.courseCode = c.CourseCode 
group by c.CourseName, c.CourseCode
having COUNT(s.EmployeeNumber) > (SELECT "maximum"
                                  FROM plannedCourses p
                                  WHERE c.CourseCode = p.CourseCode);
于 2013-07-25T13:24:51.803 回答