私はデータベースに各識別子の次の2つの情報を持っています。彼らを支配する会社、小さな支配権を持っている会社。SQLマルチコンディションCTE再帰
線に沿って何か、2つのテーブル(いくつかのユニークな識別子を無視して):
団体
orgid | org_immediate_parent_orgid
1 | 2
2 | 2
3 | 1
5 | 4
関係ORGID - > org_immediate_parent_orgid意味会社は、親を持っています。ポル私は唯一org_immediate_parent_orgidその関連 - > affiliated_orgid当社は視覚的な表現があるべきアフィリエイト
を持っている - >は、企業の親会社が子会社として
org_affiliations
orgid | affiliated_orgid
2 | 3
2 | 5
4 | 1
1 | 5
ORGIDがあり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(子会社の子会社)、リレーション(2 - > 1、リレーション1 - > 3)、Aff - > Aff、リレーションシップ関係2→5)。また、それは私に2つの別のクエリが必要です。
1つの再帰的クエリですべての可能な関係をどのように取り出すことができますか?
私は識別子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の組み合わせを見つける会社からどこに単一のクエリが欲しい。最終的な予想結果は、画像の表記に従うだけです。
編集:クレイグ氏のコメント通り、私は出力を修正しました。
Edit2:良い助けに続いて、CraigとBob Jarvisは私に問題を抱え続けました。収集子会社の
、次のコードはflawlessyの作品、と私は希望として出力されます。
AFFのための同じ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
:
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
が、 "労働組合のすべてを" 持ってカント?私が行ったSQLの開発者で
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
と労働組合のすべて 『どれworkarround「を追加するだけで、400Kまで7からそれぞれのジャンプからの計画とコストを説明する』チェック?CTE内部の問題は、UNION ALLで、ですか?私はCOMP-サブサブAFF持っている、またはそれは、会社のすべての子会社または全ての関連会社
+1せん断の美しさ。私は、読んで理解し、返信するのはD – Bohemian
を聞いていませんでした! – blueomega
ITSは長すぎる:しかし、あまりにも長い...何とか説明しなければならなかった:) – Kangkan