并提前感谢大家的帮助。
我正在尝试从两个单独的查询中获取结果,并将它们包含在具有CASE语句的第三个查询中。我已经取得了一些成功,但我无法以正确的顺序呈现第三个查询的结果。这样做的目的是显示不同经理下每个部门的员工人数。到目前为止,我只能按部门单独加载经理姓名及其部门和员工部门总数。我无法弄清楚的是如何获得每个经理行中的经理姓名和员工部门。下面是我到目前为止使用的两个源查询以及带有CASE语句的查询。我也看过UNPIVOT功能但还没有成功。
a)这个简单的查询列出了每个主要经理的名字。稍后还将使用层次结构查询返回子管理器。
select name from employees "Boss" where employeeid in
(‘1’,'5','25','84','85');
b)此查询返回每个主要经理(“1”,“5”,“25”,“84”,“85”)以及所有子经理的部门ID计数。
select departmentid, count(departmentid) COUNT from employees
where departmentid = departmentid and level <= 3
connect by prior employeeid = bossid
start with employeeid = 5
group by departmentid
order by departmentid;
c)这是一个CASE语句,输出完全符合要求。这里的问题是select语句当前只将管理器名称和管理器部门输出到列中。我需要做的是将经理姓名和经理的员工部门计数输出到各个经理行列中。我试图单独选择经理名称以获得“老板”列,另一个选择包括部门计数。但那变得混乱了。同样在第二个语句中传递计数会产生额外的不需要的列。
select e.name "Boss",
COUNT(CASE WHEN d.departmentid = '1' THEN 1 END) AS "Finance",
COUNT(CASE WHEN d.departmentid = '2' THEN 1 END) AS "HR",
COUNT(CASE WHEN d.departmentid = '3' THEN 1 END) AS "IT",
COUNT(CASE WHEN d.departmentid = '4' THEN 1 END) AS "Marketing",
COUNT(CASE WHEN d.departmentid = '5' THEN 1 END) AS "Sales"
from employees e, departments d
where e.employeeid in (select distinct e.bossid from employees e)
and e.departmentid = d.departmentid (+)
group by e.name
order by e.name;
Boss Finance HR IT营销销售
-------------------- ---------- ---------- ---------- ---------- ----------
Baxter Carney 0 0 0 0 1
Blythe Pierce 0 0 0 0 1
这是一个更改的CASE查询,用于加载员工部门计数,但遗憾的是,它是由部门而非个别经理加载的。这就是我现在所困扰的问题。如何将计数传递给正确的经理并进入右栏。
select departmentid "DEPTNO",
COUNT(CASE WHEN departmentid = '1' THEN 1 END) AS "Finance",
COUNT(CASE WHEN departmentid = '2' THEN 1 END) AS "HR",
COUNT(CASE WHEN departmentid = '3' THEN 1 END) AS "IT",
COUNT(CASE WHEN departmentid = '4' THEN 1 END) AS "Marketing",
COUNT(CASE WHEN departmentid = '5' THEN 1 END) AS "Sales"
from employees
where departmentid = departmentid and level <= 3
connect by prior employeeid = bossid
start with employeeid = 5
group by departmentid
order by departmentid
/
DEPTNO Finance HR IT Marketing Sales
3 0 0 1 0 0
5 0 0 0 0 21
这是所有经理人的。您可以看到它只是不断增加个别部门的数量。
DEPTNO Finance HR IT Marketing Sales
1 4 0 0 0 0
2 0 23 0 0 0
3 0 0 20 0 0
4 0 0 0 1 0
5 0 0 0 0 28