title: postgresql complex group by in query
body: <p>I have a <strong>query</strong> that gives:</p>
  
  <pre><code>itemid deadlineneeded delievrydate quantity 
  200  15/07/15        14/07/15     5     
  200  15/07/15        14/07/15     10
  200  15/07/15        13/07/15     25
  201  15/07/15        14/07/15     30
  200  14/07/15        10/07/15     3
  201  15/07/15        15/07/15     100
  </code></pre>
  
  <p>It gives the information from multiple tables. Basically it means When items arrive to warehouse (delievrydate) and how many. The deadlineneeded means when i'm going to use it.</p>
  
  <p>My goal is to find out the total quantity of itemid that arrives 1 day before deadlineeded.</p>
  
  <p>for example with the last data I want to get:</p>
  
  <pre><code>itemid deadlineneeded    quantity
  200         15/07/15      43 (5+10+25+3)  
  200         14/07/15      3    //5+10+25 not included because deliverydate
  201         15/07/15     30    //100 not included because deliverydate
  </code></pre>
  
  <p>How do i get it?</p>