我想按部门获取员工人数,我使用Oracle编写了此脚本,但它始终表示缺少表达式
表中使用的列:
department :name (the name of the department) -
depnum (the id of the department"primary key"),
employee : empnum (the id of the employee) -
depnum (the id of the department in which the employee in question is working "foreign key")
查询:
select
s.name
from
department s
inner join
employee p on s.depnum = p.depnum
group by
s.name
having
count(p.empnum) = max(select count(p.empnum)
from employee p, department s
where s.depnum = p.depnum
group by s.name) ;
答案 0 :(得分:1)
如果您想要按部门划分的员工人数,我会期望像这样:
select s.name, count(*) as num_employees
from department s inner join
employe p
on s.depnum = p.depnum
group by s.name ;
如果希望部门名称具有最多的名称,则可以使用having
子句:
select s.name, count(*) as num_employees
from department s inner join
employe p
on s.depnum = p.depnum
group by s.name
having count(*) = (select max(cnt)
from (select count(*) as cnt
from employee e2
group by e2.depnum
) e2
);
查询的问题是您尝试使用子查询的max()
。该语法是不允许的-也是不必要的。
答案 1 :(得分:0)
您的sql语句不正确,这就是它引发该错误的原因。我想您尝试了以下类似的方法
select s.name
from department s
inner join employe p on s.depnum=p.depnum
group by s.name
having count(p.empnum)=
select max(cnt) from
(
select count(p.empnum) as cnt
from employe p join department s
on s.depnum=p.depnum
group by s.name
) t;