需要创建程序来查找大多数 emps 的部门
Need to create procedure to find dept with most emps
我需要创建一个过程来查找员工最多的部门的部门名称。
不允许我使用温度。在我的课程中没有看到的表格。
下面的代码
CREATE OR REPLACE PROCEDURE grootste_dept
IS v_department departments.department_name%type;
BEGIN
SELECT department_name
INTO v_department
FROM departments d
JOIN employees e
ON d.department_id = e.department_id
GROUP BY department_name
HAVING COUNT(employee_id) = MAX(COUNT(employee_id);
DBMS_OUTPUT.PUT_LINE(v_department);
END;
/
期望值 = dept_name 来自员工最多的部门
4/1 PL/SQL: SQL Statement ignored
10/33 PL/SQL: ORA-00935: group function is nested too deeply
一种选择是根据员工人数对部门进行排名,return排名第一的部门。
SQL> create or replace procedure grootste_dept is
2 v_dname dept.dname%type;
3 begin
4 with data as
5 (select d.dname,
6 count(*) cnt,
7 rank() over (order by count(*) desc ) rn
8 from dept d join emp e on e.deptno = d.deptno
9 group by d.dname
10 )
11 select t.dname
12 into v_dname
13 from data t
14 where t.rn = 1;
15
16 dbms_output.put_line(v_dname);
17
18 exception
19 when too_many_rows then
20 dbms_output.put_line('Two or more departments have the same number of employees');
21 end grootste_dept;
22 /
Procedure created.
SQL> begin
2 grootste_dept;
3 end;
4 /
SALES
PL/SQL procedure successfully completed.
SQL>
您可以在一个简单的循环中使用 count(*) keep ( dense_rank first order by 1 desc ) over ( partition by e.department_id )
,其中 returns 所需的部门名称作为退出循环:
SQL> set serveroutput on
SQL> create or replace procedure
grootste_dept( o_department out departments.department_name%type ) is
begin
for c in
(
select d.department_name
from employees e
join departments d on d.department_id = e.department_id
order by count(*) keep ( dense_rank first order by 1 desc )
over ( partition by e.department_id )
)
loop
o_department := c.department_name;
dbms_output.put_line(o_department);
end loop;
end;
/
SQL> var o_department varchar2(100);
SQL> begin
grootste_dept(o_department => :o_department);
end;
/
PL/SQL procedure successfully completed
o_department
---------
<theDesiredDeptName>
如果您的数据库是 12c
,您将通过使用 fetch first 1 row only
获得所需的部门名称:
select d.dname
into o_department
from employees e
join dept d on d.deptno = e.deptno
order by count(*) keep (dense_rank first order by 1 ) over ( partition by e.deptno) desc
fetch first 1 row only
不使用循环语句的任何子查询。
我需要创建一个过程来查找员工最多的部门的部门名称。
不允许我使用温度。在我的课程中没有看到的表格。
下面的代码
CREATE OR REPLACE PROCEDURE grootste_dept
IS v_department departments.department_name%type;
BEGIN
SELECT department_name
INTO v_department
FROM departments d
JOIN employees e
ON d.department_id = e.department_id
GROUP BY department_name
HAVING COUNT(employee_id) = MAX(COUNT(employee_id);
DBMS_OUTPUT.PUT_LINE(v_department);
END;
/
期望值 = dept_name 来自员工最多的部门
4/1 PL/SQL: SQL Statement ignored
10/33 PL/SQL: ORA-00935: group function is nested too deeply
一种选择是根据员工人数对部门进行排名,return排名第一的部门。
SQL> create or replace procedure grootste_dept is
2 v_dname dept.dname%type;
3 begin
4 with data as
5 (select d.dname,
6 count(*) cnt,
7 rank() over (order by count(*) desc ) rn
8 from dept d join emp e on e.deptno = d.deptno
9 group by d.dname
10 )
11 select t.dname
12 into v_dname
13 from data t
14 where t.rn = 1;
15
16 dbms_output.put_line(v_dname);
17
18 exception
19 when too_many_rows then
20 dbms_output.put_line('Two or more departments have the same number of employees');
21 end grootste_dept;
22 /
Procedure created.
SQL> begin
2 grootste_dept;
3 end;
4 /
SALES
PL/SQL procedure successfully completed.
SQL>
您可以在一个简单的循环中使用 count(*) keep ( dense_rank first order by 1 desc ) over ( partition by e.department_id )
,其中 returns 所需的部门名称作为退出循环:
SQL> set serveroutput on
SQL> create or replace procedure
grootste_dept( o_department out departments.department_name%type ) is
begin
for c in
(
select d.department_name
from employees e
join departments d on d.department_id = e.department_id
order by count(*) keep ( dense_rank first order by 1 desc )
over ( partition by e.department_id )
)
loop
o_department := c.department_name;
dbms_output.put_line(o_department);
end loop;
end;
/
SQL> var o_department varchar2(100);
SQL> begin
grootste_dept(o_department => :o_department);
end;
/
PL/SQL procedure successfully completed
o_department
---------
<theDesiredDeptName>
如果您的数据库是 12c
,您将通过使用 fetch first 1 row only
获得所需的部门名称:
select d.dname
into o_department
from employees e
join dept d on d.deptno = e.deptno
order by count(*) keep (dense_rank first order by 1 ) over ( partition by e.deptno) desc
fetch first 1 row only
不使用循环语句的任何子查询。