postgresql complex group by in query

I have a query that gives:

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

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.

My goal is to find out the total quantity of itemid that arrives 1 day before deadlineeded.

for example with the last data I want to get:

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

How do i get it?


Source: sql

Leave a Reply