title: Calculate multiple values with sql with if expression
body: <p>I have a booking table with 6 columns called booked_start, booked_stop, used_start, used_stop, invoice_start, invoice_stop. The values are floats. I want to get a sum of the rows that has values above 0 but i also want it to calculate say used_stop - used_start.</p>
  
  <p>Currently im working with this:</p>
  
  <pre><code>SELECT
      room,
      IF( booked_stop_time &gt; 0, sum(booked_stop_time - booked_start_time), 0 ) as booked,
      IF( used_stop_time &gt; 0, sum(used_stop_time - used_start_time), 0 ) as used,
      IF( invoice_stop_time &gt; 0, sum(invoice_stop_time - invoice_start_time), 0 ) as invoice
  FROM bookings
  </code></pre>
  
  <p>The problem is that if expr1 returns false it'll reset the sum. I only want to add the rows value to the sum if its above 0.</p>
  
  <p>I also tried using a case but that didnt really work. Maybe i should do the calculation in php instead?</p>