SQL子选择组功能不允许

时间:2013-07-25 13:21:18

标签: sql oracle select subquery correlated-subquery

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

在我的原始查询中,我遇到以下错误: 此处不允许使用群组功能

查询:

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);

表格布局:

Table layout

我如何达到预期效果?

提前致谢!!

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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);