title: Performing a sum on rows with condition
body: <p>I am trying to create a query like this</p>
  
  <pre><code>select sum(t1.quantity) - sum(t2.quantity * SELECT CASE WHEN (condition on t3.status) THEN 1 ELSE 0 END) ),0)
  from table1 t1
  inner join table2 t2 on t1.key = t2.key
  inner join table3 t3 on t2.key = t3.key
  </code></pre>
  
  <p>Basically what it do is a subtraction between the first some and a second some of t2.quantity that are in rows that have a condition on the column t3.status (for example t3.status > 2).</p>
  
  <p>But I am getting</p>
  
  <blockquote>
    <p>Cannot perform an aggregate function on an expression containing an
    aggregate or a subquery.</p>
  </blockquote>
  
  <p>Is there a way to do this without adding extra join or other query?</p>