title: SQL Query - Sum Rows
body: <p>I am attempting to calculate a value inside a view, from 2 rows on a table.
  SQL Server.</p>
  
  <p>The table has these columns</p>
  
  <pre><code>----------------------------------------------
  |RefId | Quantity | TransferFrom | TransferTo |
  ----------------------------------------------
  |5601  | 100      | 5580         | null       |
  -----------------------------------------------
  |5850  | 200      | null         | 5601       |
  -----------------------------------------------
  </code></pre>
  
  <p>I would like to add the quantity of the current row, and the quantity of another row if the other row's TransferTo col equals the current row's RefId.</p>
  
  <p>In the view I have:</p>
  
  <pre><code>MyAmount = [Quantity] + (SELECT [Quantity] 
                           FROM MyTable  
                           WHERE [TransferTo] = [RefId])
  </code></pre>
  
  <p>However, this is returning <code>NULL</code>.  </p>
  
  <p>Would I better be using variables or a <code>CAST</code> function?</p>
  
  <p>Thanks in advance for any input</p>