where子句与connect by的交互和创建查询以获取层次结构中的下一级别

时间:2018-05-10 08:38:22

标签: sql oracle oracle11g

表格

create table temp_hierarchy_define (dept varchar2(25), parent_dept varchar2(25))
create table temp_employee (empid number(1), empname varchar2(50), dept varchar2(25), salary number(10))

数据

Select 'COMPANY' dept   , 'COMPANY' parent_dept  From Dual Union All
Select 'IT'        , 'COMPANY'  From Dual Union All
Select 'MARKET'    , 'COMPANY'  From Dual Union All
Select 'ITSEC'     , 'IT'       From Dual Union All
Select 'ITDBA'     , 'IT'       From Dual Union All
Select 'ITDBAORC'  , 'ITDBA'    From Dual Union All
Select 'ITDBASQL'  , 'ITDBA'    From Dual

select 1 empid, 'Rohan-ITDBASQL' empname   ,'ITDBASQL' dept ,10 salary from dual union all
select 2, 'Raj-ITDBAORC'     ,'ITDBAORC'  ,20 from dual union all
select 3, 'Roy-ITDBA'        ,'ITDBA'     ,30 from dual union all
select 4, 'Ray-MARKET'       ,'MARKET'    ,40 from dual union all
select 5, 'Roopal-IT'        ,'IT'        ,50 from dual union all
select 6, 'Ramesh-ITSEC'     ,'ITSEC'     ,60 from dual 

要求

总结所有IT部门的薪水:

  

CATEGORY SALARY

     

5,50

     

ITSEC,60

     

ITDBA,60

总结所有公司部门的薪水:

  

CATEGORY SALARY

     

IT,170

     

交易市场,40

总结所有ITDBA部门的薪水:

  

CATEGORY SALARY

     

3,30

     

ITDBASQL,10

     

ITDBAORC,20

您会注意到我们正在尝试根据层次结构中的下一个级别进行汇总。如果任何emp已经是该级别的一部分,那么我们需要向员工展示。

试用查询:

Select Category,sum(salary) from (
Select 
NVL((Select dept.dept from temp_hierarchy_define dept
               Where dept.parent_dept = 'IT'
               And dept.dept != 'IT' 
               Start With dept.dept = emp.dept
               Connect by NOCYCLE dept.dept = Prior dept.parent_dept
               and prior dept.dept is not null),emp.empid) category,
emp.*
From temp_employee emp
Where emp.DEPT in
(Select dept.dept from temp_hierarchy_define dept
Start With dept.dept = 'IT' 
connect by nocycle prior dept.dept = dept.parent_dept) ) Group by Category

关注&查询:

  1. 此查询是否适用于所有方案。或者有更好的方法吗?
  2. where条件如何与connect进行交互。例如,在子查询中,我们使用parent_dept = 'IT'进行过滤,但是当由某个emp开始连接时,可能有parent_dept = 'ITDBASQL',这也是IT的一部分。我很难理解工作流程。
  3. 感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

  

还是有更好的方法吗?

这是一个等效查询,每个表只需要一次表扫描。您需要确定您的查询或此查询是否更适合您的数据/索引/等。

SQL Fiddle

Oracle 11g R2架构设置

create table temp_hierarchy_define (
  dept varchar2(25), parent_dept varchar2(25));
create table temp_employee (
  empid number(1), empname varchar2(50), dept varchar2(25), salary number(10));

INSERT INTO temp_hierarchy_define( dept, parent_dept )
Select 'COMPANY'   , 'COMPANY'  From Dual Union All
Select 'IT'        , 'COMPANY'  From Dual Union All
Select 'MARKET'    , 'COMPANY'  From Dual Union All
Select 'ITSEC'     , 'IT'       From Dual Union All
Select 'ITDBA'     , 'IT'       From Dual Union All
Select 'ITDBAORC'  , 'ITDBA'    From Dual Union All
Select 'ITDBASQL'  , 'ITDBA'    From Dual;

INSERT INTO temp_employee( empid, empname, dept, salary )
select 1, 'Rohan-ITDBASQL'   ,'ITDBASQL'  ,10 from dual union all
select 2, 'Raj-ITDBAORC'     ,'ITDBAORC'  ,20 from dual union all
select 3, 'Roy-ITDBA'        ,'ITDBA'     ,30 from dual union all
select 4, 'Ray-MARKET'       ,'MARKET'    ,40 from dual union all
select 5, 'Roopal-IT'        ,'IT'        ,50 from dual union all
select 6, 'Ramesh-ITSEC'     ,'ITSEC'     ,60 from dual;

查询1

SELECT dept,
       SUM( salary )
FROM   (
  SELECT CASE
         WHEN lvl = 1 AND h.parent_dept = e.dept
         THEN CAST( e.empid AS VARCHAR2(25) )
         ELSE root_dept
         END AS dept,
         e.empid,
         e.salary
  FROM   ( SELECT CONNECT_BY_ROOT( dept ) AS root_dept,
                  h.*,
                  LEVEL AS lvl,
                  ROW_NUMBER() OVER ( PARTITION BY parent_dept ORDER BY ROWNUM ) AS rn
           FROM   temp_hierarchy_define h
           WHERE  parent_dept != dept
           START WITH h.parent_dept = 'IT'
           CONNECT BY NOCYCLE PRIOR h.dept = h.parent_dept
         ) h
         LEFT OUTER JOIN
         temp_employee e
         ON (  h.dept = e.dept
            OR ( h.parent_dept = e.dept AND h.lvl = 1 AND h.rn = 1)    
            )
)
GROUP BY dept

<强> Results

|  DEPT | SUM(SALARY) |
|-------|-------------|
| ITDBA |          60 |
|     5 |          50 |
| ITSEC |          60 |

您可以单独运行查询以了解他们正在做什么:

SELECT CONNECT_BY_ROOT( dept ) AS root_dept,
       h.*,
       LEVEL AS lvl,
       ROW_NUMBER() OVER ( PARTITION BY parent_dept ORDER BY ROWNUM ) AS rn
FROM   temp_hierarchy_define h
WHERE  parent_dept != dept
START WITH h.parent_dept = 'IT'
CONNECT BY NOCYCLE PRIOR h.dept = h.parent_dept

只列出层次结构中的所有行,并使用CONNECT_BY_ROOT来获取层次结构分支根部的部门。 LEVELROW_NUMBER()用于查找层次结构顶部的第一行。