title: Select Minimum difference of summary of two columns in T-SQL
body: <p>I need help building a query, which returns the Minimum difference between Value + another Value from same table and the other ID that gave the result (plus the sum can't be sum of the value itself)</p>
  
  <p>Table:</p>
  
  <pre><code>ID    Value
  1     1        
  2     2        
  3     5        
  4     -10      
  5     -5
  6     3
  7     -15
  </code></pre>
  
  <p>Expected result:</p>
  
  <pre><code>ID    Value    MinDif   IDofTheOtherValue
  1     1        3        2                   &lt;-- MinDif = 1 + 2 (ID 1 + ID 2)
  2     2        3        1                   &lt;-- MinDif = 2 + 1 (ID 2 + ID 1)
  3     5        0        5                   &lt;-- MinDif = 5 + -5 (ID 3 + ID 5)
  4     -10      -5       3                   &lt;-- MinDif = -10 + 5 (ID 4 + ID 3)
  5     -5       0        3                   &lt;-- MinDif = -5 + 5 (ID 5 + ID 3)
  6     3        -2       5                   &lt;-- MinDif = 3 + -5 (ID 6 + ID 5)
  7     -15      -10      3                   &lt;-- MinDif = -15 + 5 (ID 7 + ID 3)
  </code></pre>
  
  <p>Here's a query to create the table:</p>
  
  <pre><code>DECLARE @myTable TABLE(ID int, Value int)
  INSERT INTO @myTable VALUES (1, 1), (2,2), (3, 5), (4, -10), (5, -5), (6, 3), (7, -15)
  </code></pre>
  
  <p>And here's what I have tried, but this gives an SQL error (Cannot perform an aggregate function on an expression containing an aggregate or a subquery.)</p>
  
  <pre><code>SELECT m.ID, MIN(ABS(m.Value + (SELECT m2.Value FROM @myTable m2)))
  FROM @myTable m
  </code></pre>