title: Inner Joining a large query with itself
body: <p><strong>Problem:</strong></p>
  
  <p>I need to remove duplicate pairs from the result of a query 
  (same problem as described <a href="https://stackoverflow.com/questions/13041802/remove-reverse-duplicates-from-an-sql-query">here</a>)</p>
  
  <p>So if the result has (A,B), (B,A), (C,A)</p>
  
  <p>I am only interested in (A,B) and (C,A)</p>
  
  <p><strong>The Complication:</strong>
  Unlike in the linked question, the data is not available in a table to perform an self join and retrieve easily. It is more in the following state</p>
  
  <pre><code>(SELECT C1, C2 from a mind boggling number of joins and unions)
  </code></pre>
  
  <p>So I can make it a temp table as follows</p>
  
  <pre><code>SELECT T.C1, T.C2
  ((SELECT C1, C2 from a mind boggling number of joins and unions)) T1
  </code></pre>
  
  <p>I would like to perform an inner join to remove duplicate pairs as mentioned above</p>
  
  <p>So is there a way to do that in such a scenario
  Below query is syntactically wrong, but hopefully it conveys the idea</p>
  
  <pre><code>SELECT A.C1, A.C2
  ((SELECT C1, C2 from a mind boggling number of joins and unions)) T1 A
  INNER JOIN T1 B
  ON A.C1 = B.C1 AND
  A.C2 &lt; B.C2
  </code></pre>
  
  <p>I am running SQL Server 2012</p>