我们有5个级别的组织,从下到上将是(单位 - >部分 - >部门(部门) - >总部(GDep) - >部门)
我需要的是让所有组织的GDep低于GDep。
所以,如果我们有这个表
Child Parent
-------- --------
Unit1 Section1
Unit2 Section1
Unit3 Dep1
Unit4 GDep1
Section1 GDep1
Dep1 GDep2
GDep1 Sector1
GDep2 Sector1
我需要查询结果
Child Parent
-------- --------
Unit1 GDep1
Unit2 GDep1
Unit3 GDep2
Unit4 GDep1
Section1 GDep1
Dep1 GDep2
这是原始查询
select DISTINCT HAOU_PARENTS.ORGANIZATION_ID, HAOU_PARENTS.NAME, HAOU_PARENTS.TYPE, HAOU_CHILDS.ORGANIZATION_ID, HAOU_CHILDS.NAME, HAOU_CHILDS.TYPE, SYS_CONNECT_BY_PATH(HAOU_CHILDS.NAME, '#'), LEVEL
FROM per_org_structure_elements POSE
JOIN hr_all_organization_units HAOU_PARENTS ON (POSE.ORGANIZATION_ID_PARENT = HAOU_PARENTS.ORGANIZATION_ID)
JOIN hr_all_organization_units HAOU_CHILDS ON (POSE.ORGANIZATION_ID_CHILD = HAOU_CHILDS.ORGANIZATION_ID)
WHERE 1=1
AND pose.org_structure_version_id = 2061
START WITH UPPER(HAOU_PARENTS.TYPE) = 'GDEP'
CONNECT BY PRIOR POSE.ORGANIZATION_ID_CHILD = POSE.ORGANIZATION_ID_PARENT
ORDER BY 4
;
结果远离期望的输出,几乎是随机的。
" per_org_structure_elements "包含组织层次结构," hr_all_organization_units "包含组织详细信息,如名称和类型。
答案 0 :(得分:1)
假设您的表格有org_type
列,该列使用您为各种组织级别描述的值,您可以执行以下操作:
select child, connect_by_root(child) as gdep
from your_table
where org_type in ('Unit', 'Section', 'Dep')
connect by parent = prior child
start with org_type = 'GDep';
The connect_by_root()
clause为您提供来自层次结构根目录的子值,该值将是start with
行的GDep。
由于层次结构未在任何地方定义(我们知道),您可以明确列出您想要包含的较低级别,但如果它定义得很明确,则不需要,因为孩子们应该“降低”'反正。
使用CTE中的示例数据进行演示,并添加org_type
值:
with your_table(child, parent, org_type) as (
select 'Unit1', 'Section1', 'Unit' from dual
union all select 'Unit2', 'Section1', 'Unit' from dual
union all select 'Unit3', 'Dep1', 'Unit' from dual
union all select 'Unit4', 'GDep1', 'Unit' from dual
union all select 'Section1', 'GDep1', 'Section' from dual
union all select 'Dep1', 'GDep2', 'Dep' from dual
union all select 'GDep1', 'Sector1', 'GDep' from dual
union all select 'GDep2', 'Sector1', 'GDep' from dual
)
select child, connect_by_root(child) as gdep
from your_table
where org_type in ('Unit', 'Section', 'Dep')
connect by parent = prior child
start with org_type = 'GDep';
CHILD GDEP
-------- --------
Section1 GDep1
Unit1 GDep1
Unit2 GDep1
Unit4 GDep1
Dep1 GDep2
Unit3 GDep2
根据您添加的查询,相应的是(我认为,没有看到数据):
connect_by_root(haou_parents.name) as gdep
根据您的样本提供更多补充数据:
with per_org_structure_elements(org_structure_version_id, organization_id_child,
organization_id_parent) as (
select 2061, 1, 5 from dual
union all select 2061, 2, 5 from dual
union all select 2061, 3, 6 from dual
union all select 2061, 4, 7 from dual
union all select 2061, 5, 7 from dual
union all select 2061, 6, 8 from dual
union all select 2061, 7, 9 from dual
union all select 2061, 8, 9 from dual
),
hr_all_organization_units(organization_id, name, type) as (
select 1, 'Unit1', 'Unit' from dual
union all select 2, 'Unit2', 'Unit' from dual
union all select 3, 'Unit3', 'Unit' from dual
union all select 4, 'Unit4', 'Unit' from dual
union all select 5, 'Section1', 'Section' from dual
union all select 6, 'Dep1', 'Dep' from dual
union all select 7, 'GDep1', 'GDep' from dual
union all select 8, 'GDep2', 'GDep' from dual
union all select 9, 'Sector1', 'Sector' from dual
)
select distinct haou_parents.organization_id,
haou_parents.name,
haou_parents.type,
haou_childs.organization_id,
haou_childs.name,
haou_childs.type,
sys_connect_by_path(haou_childs.name, '#') as path,
connect_by_root(haou_parents.name) as gdep,
level
from per_org_structure_elements pose
join hr_all_organization_units haou_parents
on (pose.organization_id_parent = haou_parents.organization_id)
join hr_all_organization_units haou_childs
on (pose.organization_id_child = haou_childs.organization_id)
where 1=1
and pose.org_structure_version_id = 2061
start with upper(haou_parents.type) = 'GDEP'
connect by prior pose.organization_id_child = pose.organization_id_parent
order by 4;
得
ORGANIZATION_ID NAME TYPE ORGANIZATION_ID NAME TYPE PATH GDEP LEVEL
--------------- -------- ------- --------------- -------- ------- -------------------- -------- ----------
5 Section1 Section 1 Unit1 Unit #Section1#Unit1 GDep1 2
5 Section1 Section 2 Unit2 Unit #Section1#Unit2 GDep1 2
6 Dep1 Dep 3 Unit3 Unit #Dep1#Unit3 GDep2 2
7 GDep1 GDep 4 Unit4 Unit #Unit4 GDep1 1
7 GDep1 GDep 5 Section1 Section #Section1 GDep1 1
8 GDep2 GDep 6 Dep1 Dep #Dep1 GDep2 1