Sql 列出员工人数最少的部门名称
有两个表,员工和部门:Sql 列出员工人数最少的部门名称,sql,oracle-sqldeveloper,Sql,Oracle Sqldeveloper,有两个表,员工和部门: 我如何连接这两个表并选择员工人数最少的部门?如果您提供了表结构,回答这个问题会更好 但是试一下: SELECT deptname FROM dept WHERE deptid = (SELECT distinct deptid FROM employee ORDER BY COUNT(dept) limit 1); 我之所以回答这个问题,是因为不需要MIN,LIMIT也不是Oracle: select
我如何连接这两个表并选择员工人数最少的部门?如果您提供了表结构,回答这个问题会更好 但是试一下:
SELECT deptname
FROM dept
WHERE deptid = (SELECT distinct deptid
FROM employee
ORDER BY COUNT(dept) limit 1);
我之所以回答这个问题,是因为不需要MIN,LIMIT也不是Oracle:
select d.*
from (select deptid, count(*) as cnt
from employees e
group by deptid
order by count(*) asc
) d
where rownum = 1;
在Oracle 12C+中,您不需要子查询:
select deptid, count(*) as cnt
from employees e
group by deptid
order by count(*) asc
fetch first 1 row only;
请尝试以下方法
SELECT deptid,
deptname,
employeeCount
FROM
(
SELECT dept.deptid AS deptid,
dept.deptname,
COUNT( dept.deptid ) AS employeeCount
FROM dept
JOIN employee ON dept.deptid = employee.deptid
GROUP BY dept.deptid,
dept.deptname
)
GROUP BY deptid
HAVING employeeCount = MIN( employeeCount );
此语句以内部查询开始,该查询在共享字段deptid上将dept连接到employee。然后,它按部门对结果行进行分组,并将部门id和名称以及该部门的员工计数返回外部查询
外部查询再次按部门对数据进行分组,然后选择员工数等于最低员工数的部门的详细信息
如果您有任何问题或意见,请随时发表相应的意见。嗨:所以我的答案有点难看,而且充满了嵌套查询。但是我测试了它,它对我有效
-- First I created a couple of test tables and added a few records
drop table dept;
drop table employee;
create table dept (deptid number primary key, deptname varchar(20));
create table employee(employee_id number primary key, names varchar(20),
deptid number,foreign key (deptid) references dept(deptid));
insert into dept values(1,'HR');
insert into dept values(2,'Finance');
insert into dept values(3,'IT');
insert into employee values(1,'Tina',1);
insert into employee values(2,'Rob',1);
insert into employee values(3,'Lisa',1);
insert into employee values(4,'Will',2);
insert into employee values(5,'Lina',2);
insert into employee values(6,'Ethel',2);
insert into employee values(7,'Trevor',1);
insert into employee values(8,'Alanea',1);
insert into employee values(9,'Matthew',1);
insert into employee values(10,'Maddie',3);
insert into employee values(11,'Anna',1);
-- According to the added records, the answer we are looking for should be
the department name IT
-- select the department name from department table
select d.deptname from dept d,
/* This is where it gets ugly - basically, it counts the number of
employees in each department, then finds the id of the department that had
the smallest count */
(select deptid from
(select count(deptid) as counter, deptid from employee group by deptid)
where counter =( select min(counter)from
(select count(deptid) as counter, deptid from employee group by deptid))) minid
-- join the tables using deptid
where d.deptid = minid.deptid;
即使在我更改记录以使财务成为正确答案时,此查询也为我提供了正确答案。
如果您有任何问题,请通过评论向我大喊:带临时工
select department_name, count(employee_id)
from department d
inner join employee e
on d.employee_id = e.employee_id
having count(employee_id) =
(
select min(count(employee_id)) /*This query returns minimum count*/
from department d
inner join employee e
on d.employee_id = e.employee_id
group by department_name
)
group by department_name;
像
选择e1.部门id,计数1.员工id emp\U计数
来自人力资源部员工e1
按e1.U部门id分组
选择d1.部门名称,t1.环境管理人员人数员工人数
从温度t1
,人力资源部d1
其中t1.department\u id=d1.department\u id+
而且不存在
选择1
从温度t2开始
其中t2.emp_计数