@Martinの答えは正しいです。彼は天才です。
ゴー・マーティン!
訂正
私が与えた双方向ソリューションに対して実行すると、彼の答えは1つの小さな変更で機能します。それ以外の場合、結果は正しくありません。
だからあなたの答えは彼と私のものです:)
完全な解決策:
DECLARE @T1 TABLE (calling_party VARCHAR(50), called_party VARCHAR(50)) INSERT INTO @T1 SELECT * FROM dbo.monthly_connections_test INSERT INTO @T1 SELECT * FROM ( SELECT called_party AS calling_party, calling_party AS called_party FROM dbo.monthly_connections_test AS T2 WHERE T2.called_party < T2.calling_party ) T2 WHERE NOT EXISTS ( SELECT * FROM monthly_connections_test WHERE calling_party = T2.calling_party and called_party = T2.called_party ) select u1, u2, count(called_party) called_parties from ( select distinct u1, u2, called_party from ( select a1.calling_party u1, a2.calling_party u2 from (select calling_party from @T1 group by calling_party) a1, (select calling_party from @T1 group by calling_party) a2 ) pairs, @T1 AS T where (u1 <> u2) and ((u1 = t.calling_party and u2 <> t.called_party) or (u2 = t.calling_party and u1 <> t.called_party)) ) res group by u1, u2 order by u1, u2
プレ>