I am making a SQL query to find the products in a database that are below ROP(reorder point) and that belong to a specific production cell.
To do this I have to do several operations on my selected columns, and I have to join multiple tables.
Here is the SQL that I have written:
Select Statement
SELECT it.ItemID, descr.ShortDesc, CONVERT(INT,inv.OrderPoint) AS ROP,
CONVERT(INT, (bin.QtyOnHand - inv.QtyOnSO + inv.QtyOnPO)) AS Avail,
CONVERT(INT, bin.QtyOnHand) AS QOH, CONVERT(INT,inv.QtyOnSO) AS Sold,
CONVERT(INT,((bin.QtyOnHand - inv.QtyOnSO)/(sale.PrevYearQtySold/10)*100)) AS percentUsage,
CONVERT(INT, sale.PrevYearQtySold/10) AS moUsage, CONVERT(INT,inv.MaxStockQty) AS Bin,
CONVERT(INT, inv.OrderPoint - (bin.QtyOnHand - inv.QtyOnSO)) AS NTS
FROM timItem AS it
Table Joins
LEFT JOIN timInventory AS inv
ON it.ItemKey = inv.ItemKey
LEFT JOIN timItemClass AS itClass
ON it.ItemClassKey = itClass.ItemClassKey
LEFT JOIN timItemDescription AS descr
ON it.ItemKey = descr.ItemKey
LEFT JOIN texItemSalesSummary AS sale
ON it.ItemKey = sale.ItemKey
LEFT JOIN timWhseBinInvt AS bin
ON it.ItemKey = bin.ItemKey
Where Clause
WHERE (bin.QtyOnHand - inv.QtyOnSO) < inv.OrderPoint
AND itClass.ItemClassID = 'A'
ORDER BY
ORDER BY percentUsage ASC
Upon the completion of my query I get something that looks like the following:
Item ID || ShortDesc || ROP || AVAIL || QOH || SOLD || %Usage || mo Use || Bin || NTS
item descr 12 -4 47 51 -8 46 24 16
item descr 6 0 12 12 0 11 12 6
item descr 18 0 44 44 0 9 20 18
This would be fine, minus the fact that if an item in the timWhseBinInvt has a temporary bin location and it's QtyOnHand reaches zero. It's row is automatically deleted from the table (just the way that our MAS500 schema browser describes it).
Thus leaving me with an incomplete result, excluding the items that don't exist in that particular table.
I would like to see data that looks more like this:
Item ID || ShortDesc || ROP || AVAIL || QOH || SOLD || %Usage || mo Use || Bin || NTS
item descr 12 -4 47 51 -8 46 24 16
item descr 6 null null 5 null 11 12 6
item descr 18 0 44 44 0 9 20 null
This would give me all of the items in the 'A' production cell, including items that aren't found in the table timWhseBinInvt. The columns corresponding with that table should have a value of null for the items that do not exist within that table.
From my research on different types of joins. it seemed that left joins make this kind of functionality possible.
I've also looked into *is null, not exists,*etc. with no luck.
I apologize if this post is unclear to anybody, I'm very new to writing SQL queries and I thought I'd reach out on here to see if anybody with more experience could help.
Thanks