【发布时间】:2011-10-20 18:03:01
【问题描述】:
在数据库中,每个标识符都有以下 2 条信息。控制他们的公司,以及他们有少量控制权的公司。
类似的东西,2 个表(忽略一些唯一标识符):
组织
orgid | org_immediate_parent_orgid
1 | 2
2 | 2
3 | 1
5 | 4
关系 orgid --> org_immediate_parent_orgid 表示公司有父级。告诉我它的唯一相关 org_immediate_parent_orgid --> 公司的母公司作为子公司的 orgid
org_affiliations
orgid | affiliated_orgid
2 | 3
2 | 5
4 | 1
1 | 5
orgid --> associated_orgid 是公司有附属公司
视觉表示应该是这样的:
关于组织的红色关系,关于蓝色关系org_affiliations。
如果想要让 2(或 2 的子公司)拥有的所有公司都参与其中:
select m.org_immediate_parent_orgid
,m.orgid
from oa.organizations m
where m.org_immediate_parent_orgid is not null
start with m.orgid in (Identifiers)
connect by nocycle prior m.orgid=m.org_immediate_parent_orgid
返回
org_immediate_parent_orgid| orgid
1 | 2
2 | 2
3 | 1
如果想要让所有公司都 2(或 2 的附属儿子)参与其中:
select aff.orgid,aff.affiliated_orgid
from oa.org_affiliations aff
where aff.affiliated_orgid is not null
start with aff.orgid in(Identifiers)
connect by nocycle prior aff.affiliated_orgid =aff.orgid
返回
orgid | affiliated_orgid
2 | 3
2 | 5
所有可能的关系:
- Aff --> Aff
- Aff --> 子
- 子 --> Aff
- 子 --> 子
我只找到Sub --> Sub(子公司的子公司),relationship (2 --> 1 andrelationship 1 --> 3) and Aff --> Aff,relationship (2 --> 3 andrelationship 2 - -> 5)。它还需要我 2 个单独的查询。
如何在一个递归查询中提取所有可能的关系?
如果我通过标识符 2,则应该有可能返回以下内容:
Relation | Loop| orgid | children
Sub | 1 | 2 |2
Sub | 1 | 2 |1
Aff | 1 | 2 |3
Aff | 1 | 2 |5
Sub | 2 | 1 |3
Aff | 2 | 1 |5
在每个周期中都会检查每个标识符的子和附属。对新的孩子重复。
知道如何处理它吗?
TL:DR: 2 个表(子公司\附属公司),2 个查询。想要一个查询,我从一家公司那里找到所有子公司和附属公司以及所有可能的 subs\affs 组合。最终预期结果展示,按照图片表示即可。
编辑:正如 Craig 所说,我修复了输出。
Edit2:在 Craig 和 Bob Jarvis 给予的良好帮助之后,我继续遇到问题。
对于收集子公司,以下代码完美无缺,输出如我所愿:
with
relations as
(
select orgid as children,org_immediate_parent_orgid as orgid,'Sub' as relation
from oa.organizations
)
select distinct relation, level, orgid, children
from relations
where children is not null
start with orgid in (identifier)
connect by
nocycle prior children = orgid
order by 2,3,4
AFF 也一样:
with
relations as
(
select affiliated_orgid as children, orgid as orgid,'Aff' as relation
from oa.org_affiliations
)
select distinct relation, level, orgid, children
from relations
where children is not null
start with orgid in (identifier)
connect by
nocycle prior children = orgid
order by 2,3,4
但不能有“联合所有”?
with
relations as
(
select orgid as children,org_immediate_parent_orgid as orgid,'Sub' as relation
from oa.organizations
UNION ALL
select affiliated_orgid as children, orgid as orgid,'Aff' as relation
from oa.org_affiliations
)
select distinct relation, level, orgid, children
from relations
where children is not null
start with orgid in (identifier)
connect by
nocycle prior children = orgid
order by 2,3,4
在 sql developer 中,我检查了“解释从 7 到 400k 的每次跳跃的计划和成本,只需添加“union all”。任何解决方法?问题是在 CTE 内部,在 union alL 中吗?
Bob Jarvis 解决方案在我有 comp-sub-sub-aff 的情况下不起作用,或者它找到公司的所有子公司或所有附属公司
【问题讨论】:
-
+1 剪裁美。但是,太长了……没听:)
-
太长了,我无法阅读、理解和回复!
-
TL:DR: 2 个表(子公司\附属公司),2 个查询。想要一个查询,我从一家公司那里找到所有子公司和附属公司以及所有可能的 subs\affs 组合。最终预期结果展示,按照图片表示即可。
-
@blueomega - 你有没有想过这个问题,或者你还有问题吗?如果还是不行,我再看看。
标签: sql recursion plsql common-table-expression recursive-query