I have two tables: StorageTransactions
and FutureStockUsageMaterials
.
The StorageTransactions
table shows all the movement of the products in and out of the storage, while the FutureStockUsageMaterials
table shows possible future movements of products in and out of the tables.
I've written the following query:
SELECT
SUM(StorageTransactions.Quantity) as CurrentStock,
COALESCE(FutureStockUsageMaterials.FutureStock, 0) as FutureStock,
StorageTransactions.products_ProductId
FROM StorageTransactions
LEFT JOIN FutureStockUsageMaterials
ON FutureStockUsageMaterials.products_ProductId = StorageTransactions.products_ProductId
GROUP BY StorageTransactions.products_ProductId`
For example, if product with the product id of 3
will be used in the future, but there is no record of it being in the older transactions, what I would like to see is a line like this:
CurrentStock | FutureStock | products_ProductId
0 | -325.00 | 3
This query works as expected, as in showing 3 columns, the first being the current stock of a product, the second being the future stock of the product, and the third being the product itself. My problem is, that when there is no entry for a given product in the StorageTransactions
table, but in the future that product should be used, this query does not return that row, I am presuming because of my join.
How could I achieve the desired behaviour, ie. getting all the products that will be used in the future?