SELECT d.DEPARTMENT_NAME
FROM Department d,
Student s
WHERE d.DEPARTMENT_ID = s.DEPARTMENT_ID
GROUP BY d.DEPARTMENT_NAME
HAVING COUNT(s.STUDENT_ID) < MAX(COUNT(s.STUDENT_ID));
该代码用于加入学生和部门表。 department_id
是部门与学生之间的关键。需要找到没有最大学生人数的部门。错误是组函数嵌套太深。嵌套是否允许最多3个?
这是确切的错误
ORA-00935:群组功能嵌套太深
答案 0 :(得分:0)
UPD 。哦,石〜!我刚刚提到了与Oracle有关的问题。我不知道rank()
函数的语法,但我认为它应该非常接近Sql Server。
这是:
;with Department(DEPARTMENT_ID, DEPARTMENT_NAME) as (
select 1, 'first' union all
select 2, 'second' union all
select 3, 'third'
)
, Student(STUDENT_ID, DEPARTMENT_ID) as (
select 1, 1 union all
select 2, 2 union all
select 3, 2 union all
select 4, 2 union all
select 5, 3 union all
select 6, 3 union all
select 7, 3
)
, DepOrdered as (
select
d.DEPARTMENT_ID,
d.DEPARTMENT_NAME,
s.StudentCnt,
-- rank departments by the number of students
rank() over (order by s.StudentCnt desc) as Rnk
from Department d
cross apply (
-- for every department count its students
select
count(s.STUDENT_ID) StudentCnt
from Student s
where
d.DEPARTMENT_ID = s.DEPARTMENT_ID
) s
)
select
DEPARTMENT_ID,
DEPARTMENT_NAME,
StudentCnt
from DepOrdered
where
-- Rnk = 1 would have all departments with max number of students
Rnk > 1