Let's suppose i have the following table.
|item_no | po_num | line_num | recv_qty | req_qty |
|--------|--------|----------|----------|---------|
|123 | 001 | 10 | 10 | 10 |
|234 | 001 | 20 | 25 | 30 |
|345 | 001 | 30 | 80 | 80 |
|345 | 001 | 30 | -80 | 80 |
|567 | 002 | 10 | 40 | 10 |
|789 | 002 | 20 | 55 | 30 |
|987 | 002 | 30 | -70 | 70 |
In this case, what this represents is that, for item 123 one purchase order (po_num) was created for items 123, 234 and 345. There is a different line_no for each item.
In the case of the po_num = 001 the line_num was returned to the vendor and so there are two entries with po_num = 001 and line_num = 30 one with a negative rev_qty indicating the order was returned.
There is also one case, for item 987 that there is only one entry with a negative rev_qty indicating that the order was returned.
What i need is a query that is able to give me the sum of the recv_qty and the req_qty so i can get the ratio and see if the providers are fulfilling the purchase orders properly. I need to be able of handling the two cases. I believe that in the second case it is enough if i add a "where recv_qty >=0" since i am interested in knowing if a provider did not deliver the order, not being necessarily returned. I am having creating a query that handles the two cases. Any help will be appreciated.
Not really sure what you are looking but here is a guess.
declare @Something table
(
item_no int
, po_num char(3)
, line_num int
, recv_qty int
, req_qty int
)
insert @Something
values
(123, '001', 10, 10, 10)
,(234, '001', 20, 25, 30)
,(345, '001', 30, 80, 80)
,(345, '001', 30, -80, 80)
,(567, '002', 10, 40, 10)
,(789, '002', 20, 55, 30)
,(987, '002', 30, -70, 70)
select item_no
, po_num
, sum(case when recv_qty > 0 then recv_qty else 0 end) as QuantitySold
, sum(case when recv_qty < 0 then ABS(recv_qty) else 0 end) as QuantityReturned
from @Something
group by item_no
, po_num
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.