title: SUM a column then SUBTRACT to the SUM of another column
body: <p>I have a table that has this fields:</p>
  
  <pre><code>EmpID,
  Code,
  Amount,
  TransDate,
  CM,
  CMDate
  </code></pre>
  
  <p>What I want to get to a datagrid, is to <code>SUM</code> all the <code>Amount</code> that has the same <code>Code</code> and <code>SUM</code> the <code>CM</code> that has the same <code>Code</code> as the <code>Amount</code> then subtract the <code>SUM(Amount)</code> to the <code>SUM(CM)</code> but if the Result IS <code>NULL</code> (e.g. there is no <code>CM</code> to SUM and Subtract to the <code>SUM(Amount)</code>) then it just have to select the <code>SUM(Amount)</code>. I came up with this statement:</p>
  
  <pre><code>SELECT
      TransDate, 
      Code, 
      SUM(Amount) - SUM(CM) AS 'Result'
  FROM DeductionTbl
  WHERE 
      (EmpID = @empID)
  GROUP BY Code, TransDate
  ORDER BY TransDate
  </code></pre>
  
  <p>I get the Result value if there is a <code>SUM(CM)</code> to subtract to the <code>SUM(Amount)</code> but I get the Result <code>NULL</code> value if there is no <code>SUM(CM)</code> of the same <code>Code</code>. I don't want the <code>NULL</code> value on the datagrid. If there is a <code>NULL</code> value just return the <code>SUM(Amount)</code>. Is there a way to do that? Thanks!</p>