title: PostgreSQL: having clause not working as expected
body: <p>I have a user <strong>DE9013</strong> with two positive ratings in an SQL-table:</p>
  
  <pre><code># select * from pref_rep where id='DE9013';
     id   | author | good | fair | nice | about |         last_rated         |   author_ip
  --------+--------+------+------+------+-------+----------------------------+---------------
   DE9013 | DE9241 | t    | t    | t    |       | 2011-03-06 09:23:00.400518 | 97.33.154.43
   DE9013 | DE9544 | t    | t    | t    |       | 2011-03-06 10:06:37.561277 | 97.33.35.54
  (2 rows)
  </code></pre>
  
  <p>And the total sum of <strong>fair</strong> + <strong>nice</strong> ratings is as expected four:</p>
  
  <pre><code># select
  count(nullif(r.fair, false)) +
  count(nullif(r.nice, false)) -
  count(nullif(r.fair, true)) -
  count(nullif(r.nice, true))
   from pref_rep r where id='DE9013';
   ?column?
  ----------
          4
  (1 row)
  </code></pre>
  
  <p>My question is: why do I get the user <strong>9013</strong> in the list below, where I'm trying to find all users who played more than 30 completed games and have the ratings (<strong>fair</strong> + <strong>nice</strong>) higher than 30?</p>
  
  <pre><code># select substring(m.id from 3)::bigint, 3
  from pref_match m, pref_rep r
  where m.id=r.id and
  m.id like 'DE%'
  group by m.id
  having (sum(m.completed) &gt; 30 and
  count(nullif(r.fair, false)) +
  count(nullif(r.nice, false)) -
  count(nullif(r.fair, true)) -
  count(nullif(r.nice, true)) &gt; 30) limit 3;
   substring | ?column?
  -----------+----------
        9013 |        3
        8692 |        3
        7059 |        3
  (3 rows)
  </code></pre>
  
  <p>Using PostgreSQL 8.4.7 with CentOS 5.7 / 64 bit</p>