title: sql join table with its subtable
body: <p>Suppose the following example table:</p>
  
  <pre><code>+-----+-----+-----+
  | ID1 | ID2 | VAL |
  +-----+-----+-----+
  | A   | X   | 1   |
  | A   | Y   | 2   |
  | A   | Z   | 3   |
  | B   | X   | 4   |
  | B   | Z   | 5   |
  | C   | Y   | 6   |
  +-----+-----+-----+
  </code></pre>
  
  <p>Say I am interested in <code>ID1 = B</code> and would like compare it to others. The desired output would be:</p>
  
  <pre><code>+---------+---------+---------+---------+---------+---------+---------+
  | REF.ID1 | OTH.ID1 | REF.ID2 | REF.ID2 | REF.VAL | OTH.VAL |  DIFF   |
  +---------+---------+---------+---------+---------+---------+---------+
  | B       | A       | X       | X       | 4       | 1       |  3      |
  | B       | A       | NULL    | Y       | NULL    | 2       | -2      |
  | B       | A       | Z       | Z       | 5       | 3       |  2      |
  | B       | B       | X       | X       | 4       | 4       |  0      |
  | B       | B       | Z       | Z       | 5       | 5       |  0      |
  | B       | C       | NULL    | Y       | NULL    | 6       | -6      |
  +---------+---------+---------+---------+---------+---------+---------+
  </code></pre>
  
  <p>Thank you.</p>