title: Sum(IF) in mysql
body: <p>Can I get the sum of bug count with multiple conditions in mysql?</p>
  
  <p>My requirement is, I need to get the sum or the count of bugids having some priority, say P1, which exceed a particular time, but the exceeded count should be calculated from the total count of that particular Priority.</p>
  
  <p>My query is:-</p>
  
  <pre><code>select 
  sum(
   IF(priority="P1",1,0)) P1, 
  sum(
   IF(timediff(delta_ts,creation_ts) &gt; "00:00:05",1,0))P1_exeeded,
  SUM(
   IF(priority="P2",1,0)) P2,
  sum(
   IF(timediff(delta_ts,creation_ts) &gt; "00:00:10",1,0))P2_exeeded,
  SUM(
   IF(priority="P3",1,0)) P3count,
  SUM(
   IF(priority="P4",1,0)) P4count,
  SUM(
   IF(priority="P5",1,0)) P5count,
  SUM(
   IF(priority="P6",1,0)) P6count,
  SUM(
   IF(priority="P7",1,0)) P7count,
  SUM(
   IF(priority="P8",1,0)) P8count 
  from bugs 
  where bugs.product_id=237 
  and bugs.resolution='FIXED' 
  and bugs.creation_ts &gt;='2013-06-14 09:00:00' 
  and bugs.creation_ts &lt;= '2013-06-16 08:59:59' 
  and bug_status="RESOLVED";
  </code></pre>
  
  <p>I got the result as:-</p>
  
  <pre>
  ------+------------+------+------------+---------+---------+---------+---------+---------+---------+
  | P1   | P1_exeeded | P2   | P2_exeeded | P3count | P4count | P5count | P6count | P7count | P8count |
  +------+------------+------+------------+---------+---------+---------+---------+---------+---------+
  |    7 |         19 |    6 |         18 |       5 |       1 |       0 |       0 |       0 |       0 |
  +------+------------+------+------------+---------+---------+---------+---------+---------+---------+
  1 row in set (0.00 sec)
  
  </pre>
  
  <p>But I need the sum of the exceeded bug ids from each priority, by taking two conditions in :- </p>
  
  <p><strong>sum of exceeded bugids above a particular time limit, and its taken from the count of its particular priority category.</strong></p>
  
  <p>the exceeded sum of bug ids should satisfy both condition in each case.</p>
  
  <p>P1 priority bug ids total I have.
  P1_exeeded = IF(timediff(delta_ts,creation_ts) > "00:00:05",1,0)) taken from the the count of P1 bugids.</p>
  
  <p>Please help to use sum( satisfying 2 conditions) in mysql</p>