I have two tables, one has products shipped in and other table where product is shipped out. I just need to have a total number of product still not shipped. It can be easily done by calculating the difference but I am facing some issues.
Stock In Flow Table:
Product ID | Quantity In |
---|---|
1 | 15 |
1 | 5 |
2 | 5 |
2 | 10 |
3 | 15 |
4 | 5 |
Stock Out Flow Table:
Product ID | Quantity Out |
---|---|
1 | 7 |
2 | 3 |
3 | 5 |
2 | 2 |
1 | 8 |
2 | 2 |
1 | 5 |
3 | 3 |
I am using this query
SELECT
"Stock In Flow Table"."Product ID" 'Product ID',
sum("Stock In Flow Table"."Quantity In") as "Quantity In",
sum("Stock Out Flow Table"."Quantity Out") as "Quantity Out",
"Quantity In" -"Quantity Out" as "InStock"
FROM "Stock In Flow Table"
JOIN "Stock Out Flow Table" ON "Stock Out Flow Table"."Product ID" = "Stock In Flow Table"."Product ID"
GROUP BY "Product ID", "InStock"
The desired result should be this
Product ID | InStock |
---|---|
1 | 0 |
2 | 8 |
3 | 7 |
4 | 5 |
However The numbers are not correct. It is adding up all the numbers multiple times. I have tried multiple joins but still not getting the desired result. Please help me to check where I am going wrong?