title: MySQL: IF statement used with SUM
body: <p>I have the following table:</p>
  
  <pre><code>Unique | Common |  Score
  1      |   1    |   10  
  2      |   1    |   10  
  3      |   1    |   10
  4      |   1    |   -10
  </code></pre>
  
  <p>If I run this:</p>
  
  <pre><code>SELECT
      SUM(`score`) AS `total`
  FROM
      `test`
  GROUP BY
      `common`
  </code></pre>
  
  <p>I get the total:</p>
  
  <pre><code>20
  </code></pre>
  
  <p>This is what I'd expect.</p>
  
  <p>I want to treat negative numbers as zero and sum the remaining score to get a total. So, if I run this:</p>
  
  <pre><code>SELECT
      IF(`score`&lt;0, 0, SUM(`score`)) AS `total`
  FROM
      `myTable`
  GROUP BY
      `unique`
  </code></pre>
  
  <p>I get these totals:</p>
  
  <pre><code>10
  10
  10
  0
  </code></pre>
  
  <p>This is what I'd expect as well.</p>
  
  <p>But, If I run this:</p>
  
  <pre><code>SELECT
      IF(`score`&lt;0, 0, SUM(`score`)) AS `total`
  FROM
      `myTable`
  GROUP BY
      `common`
  </code></pre>
  
  <p>I get a total of:</p>
  
  <pre><code>20
  </code></pre>
  
  <p>Why do I get 20 instead of 30 when performing the last query? The only difference is the grouping. </p>
  
  <p>BTW, removing the superflous <code>Group by</code> clause from the last query also gives a result of 20.</p>
  
  <p>Thanks in advance.</p>