繁体   English   中英

使用Oracle的SQL中缺少表达式问题

[英]Missing expression problem in SQL using Oracle

我想按部门获取员工人数,我使用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) ;

如果您想要按部门划分的员工人数,则可能会发生以下情况:

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() 该语法是不允许的-也是不必要的。

您的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;

暂无
暂无

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

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