Sql 如何查询某一级别的上级组织?
我们有5个级别的组织,从下到上依次为单位->部门->部门部门部门->综合部门GDep->部门 我需要的是获得低于GDep的所有组织的GDep 如果我们有这张桌子Sql 如何查询某一级别的上级组织?,sql,oracle,hierarchical-data,Sql,Oracle,Hierarchical Data,我们有5个级别的组织,从下到上依次为单位->部门->部门部门部门->综合部门GDep->部门 我需要的是获得低于GDep的所有组织的GDep 如果我们有这张桌子 Child Parent -------- -------- Unit1 Section1 Unit2 Section1 Unit3 Dep1 Unit4 GDep1 Section1 GDep1 Dep1 GDep2 GDep1 Sector1 GDep2 Sect
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
;
结果与期望的输出相差甚远,而且几乎是随机的
每个组织结构元素包含组织层次结构,人力资源所有组织单元包含组织详细信息,如名称和类型。假设您的表中有一个组织类型列,该列使用您为不同组织级别描述的值,您可以执行以下操作:
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';
为您提供层次结构根中的子值,该子值将是起始行的GDep
因为层次结构没有定义在我们知道的任何地方,所以可以明确列出要包含的较低级别,但是如果定义良好,则不需要这样做,因为子级无论如何都应该是“较低”的
使用CTE中的示例数据进行演示,并添加组织类型值:
根据您添加的查询,我认为在没有看到数据的情况下,等价物是:
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;
得到
首先,你真是太棒了第二,谢谢你的编辑和快速回答,我被困在这几个小时了答案的第二部分不是我需要的,我需要的正是你在第一部分所说的,你能解释一下我的问题是什么吗?为什么从父类型开始,而不是从子类型开始,我们会得到非常不同的结果?我不知道你的意思。您需要考虑在层次结构树中向上或向下走的方式,以及您的起点。就我所见,第二部分的逻辑与第一部分相同——对于子名称和根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