0

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?

2
  • Use GROUP BY combined with SUM. Commented Jun 23, 2015 at 8:49
  • I tried. it gives false results... to solve it I must take under considration the delievrydate and it is something that changes for each row... it's not a simple sum. Commented Jun 23, 2015 at 8:51

3 Answers 3

1
SELECT itemid, deadlineneeded, sum(quantity) AS total_quantity
FROM <your table>
WHERE (deadlineneeded - delievrydate)::int >= 1
GROUP BY 1, 2
ORDER BY 1, 2;

This uses a "delievrydate" (looks like a typo to me) that is at least 1 day before the "deadlineneeded" date, as your sample data is suggesting.

Sign up to request clarification or add additional context in comments.

Comments

0
select distinct date_trunc('day',deadlineneeded),sum from 
(select *,sum(quantity) over (partition by deadlineneeded-delievrydate) from     tablename) t 
where deadlineneeded-delievrydate = '1 day';

1 Comment

you won't see itemid, cos you summarize the quantity... you can list them though comma though...
0

Use GROUP BY combined with aggregate function SUM.

select itemid, deadlineneeded, SUM(quantity)
from tablename
where deadlineneeded = delievrydate + 1
group by itemid, deadlineneeded

The where deadlineneeded = delievrydate + 1 part is typically product specific, and I don't know postgresql... ANSI SQL has deadlineneeded = delievrydate + interval'1' day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.