我有下表。
CREATE TABLE Employee_id_credits ( Employee_id, credits ) AS
SELECT 10, 1 FROM DUAL UNION ALL
SELECT 12, 1 FROM DUAL UNION ALL
SELECT 10, 1 FROM DUAL UNION ALL
SELECT 12, 1 FROM DUAL UNION ALL
SELECT 12, 1 FROM DUAL UNION ALL
SELECT 14, 1 FROM DUAL;
以下查询对员工的信用总数进行分组和计算。
select Employee_id, count(*) as "Total_credits"
from Employee_id_credits
group by Employee_id;
给出以下输出。
Employee_id Total_credits
----------- -------------
10 2
12 3
14 1
我有一个带有层次结构的员工经理表。
CREATE TABLE Employee_Manager ( Employee_id, Manager_id ) AS
SELECT 10, 101 FROM DUAL UNION ALL
SELECT 12, 120 FROM DUAL UNION ALL
SELECT 13, 120 FROM DUAL UNION ALL
SELECT 14, 150 FROM DUAL UNION ALL
SELECT 101, NULL FROM DUAL UNION ALL
SELECT 120, 130 FROM DUAL UNION ALL
SELECT 130, NULL FROM DUAL;
我有一个查询来查找该员工的顶级经理。
SELECT
Employee_id
FROM
Employee_Manager
WHERE
Manager_id is null
CONNECT BY PRIOR
Manager_id = Employee_id
START WITH
Employee_id = '12';
我想结合上面两个查询,以便输出如下所示。如何组合两个查询?
Manager Total_credits
------- -------------
101 2
130 3
150 1
答案 0 :(得分:2)
Oracle 11g R2架构设置:
SELECT CONNECT_BY_ROOT( Employee_id ) AS Employee_id,
COALESCE( manager_id, employee_id ) AS manager_id
FROM Employee_manager
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY PRIOR Manager_id = Employee_id
查询1 - 查找每位员工的经理:
| EMPLOYEE_ID | MANAGER_ID |
|-------------|------------|
| 10 | 101 |
| 12 | 130 |
| 13 | 130 |
| 14 | 150 |
| 101 | 101 |
| 120 | 130 |
| 130 | 130 |
<强> Results 强>:
SELECT m.manager_id,
SUM( c.credits ) As total_credits
FROM Employee_id_credits c
INNER JOIN
(
SELECT CONNECT_BY_ROOT( Employee_id ) AS Employee_id,
COALESCE( manager_id, employee_id ) AS manager_id
FROM Employee_manager
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY PRIOR Manager_id = Employee_id
) m
ON ( c.employee_id = m.employee_id )
GROUP BY m.manager_id
查询2 - 将其加入信用表并汇总:
| MANAGER_ID | TOTAL_CREDITS |
|------------|---------------|
| 101 | 2 |
| 130 | 3 |
| 150 | 1 |
<强> Results 强>:
undefined is not an object (evaluating 'navigator.userAgent.indexOf')
<unknown>
:12:71
loadModuleImplementation
require.js:213:12
<unknown>
getScrollPosition.js:12:31