title: SQL: SUMming certain items in a column and subtracting it from another figure in that column
body: <p>Sorry about the title. It might be a bit confusing! The sample table I'm working with is given below:</p>
  
  <pre><code>     ID         Quantity         Type
   -----------------------------------------------
       1             14             PO
       1              2             PO  
       1              4             MH  
       1              3             MH
       1              2             MH
       2             16             PO
       2             12             MH
       2              9             MH
  </code></pre>
  
  <p>Here's what I want to do. I want to sum all quantities of ID = 1 and Type = PO (14 + 2) as SUM_IN. I then want to sum all quantities of ID = 1 and Type = MH (4 + 3 + 2) as SUM_OUT. Once I have this done I want to compare the two and return values only where SUM_OUT > SUM_IN. So for ID = 1 would not be returned where as ID = 2 would, because (12 + 9)  > 16.</p>
  
  <p>Is there a way to do this in SQL or will I need to use PL/SQL and variables for the task. I have very little experience in PL/SQL, but logically it seems that variables would be the easiest way to solve the problem. I know that select statements can be stored in variables but I'm not sure how to. Here are my two SQL selects anyway</p>
  
  <pre><code>  SELECT SUM(QUANTITY) AS SUM_IN
    FROM TRANSLOG
    WHERE TYPE IN ('PO')
    AND ID = '1'
  
    SELECT SUM(QUANTITY) AS SUM_OUT
    FROM TRANSLOG
    WHERE TYPE IN ('MH')
    AND ID = '1'
  </code></pre>
  
  <p>So if I could set both these to variables, the task shouldn't be too difficult, right???</p>
  
  <p>Thanks in advance for the help.</p>