2017-07-06 10 views
3

は、私は2つの列を持つデータセットを持っています少なくとも2つの異なるメンバーを有する複数のペア。だから、最終的な結果は返す必要があります。どちらも、他のリンク Dwight Schrute --> AngelaAngela --> Dwight Schruteが原因で削除されていないため、T-SQL検索ペア

source   target   
Jim Halpert  Pam Beasley 
Jim Halpert  Pam Beasley 
Kevin Malone Stanley Hudson 
Kevin Malone Ryan Howard 
Pam Beasley  Oscar 

Michael --> Kellyは、削除された複数のリンクがありますが、リンクは同じメンバー間です。 Erin --> MeredithErin --> Meredithは、リンクが同じメンバー間にあるため(同じ方向ですが)削除されています。

は、私はどちらかの方向に同じメンバーを伴う明確なリンクを見つける方法を知っている:

select source 
     ,target 
from dbo.networktest 
group by source, target 
having count(*) > 1 
union 
select b.source 
     ,b.target 
from dbo.networktest a 
left outer join dbo.networktest b on a.source = b.target and a.target = b.source 
where b.source is not null and b.target is not null 

どのように変化するであろう(またはスクラップ/再構築)それは私の目的を達成するには?皆さんの洞察力に感謝します!私の質問をより明確にすることができれば、私に知らせてください。

答えて

3

私はexistsがtyouがしたい義和ないと思う:

select nt.* 
from networktest nt 
where exists (select 1 
       from networktest nt2 
       where nt2.source in (nt.source, nt.target) and 
        nt2.target not in (nt.source, nt.target) 
      ) or 
     exists (select 1 
       from networktest nt2 
       where nt2.target in (nt.source, nt.target) and 
        nt2.source not in (nt.source, nt.target) 
      ); 
+0

うわー。完全に動作します - 簡単な質問には申し訳ありません!それが許せば答えを受け入れる。 –

関連する問題