I want to update my inventory stock by adding two tables values.
The problem is that if the item doesnt exist in one from two tables it doesnt update this item. Why happen this? I'm using Full join in both two tables
WITH cte AS
(
(SELECT
InventoryItemID,
(COALESCE(SUM(CASE WHEN CustomerID = 0 THEN Quantity ELSE 0 END) -
SUM(CASE WHEN CustomerID > 0 THEN Quantity ELSE 0 END), 0)) AS Total
FROM
InventoryTransTemp
GROUP BY InventoryItemID)
), cte2 as
(
(SELECT
InventoryItemID,
(COALESCE(SUM(CASE WHEN CustomerID = 0 THEN Quantity ELSE 0 END) -
SUM(CASE WHEN CustomerID > 0 THEN Quantity ELSE 0 END), 0)) AS Total2
FROM
InventoryTrans
GROUP BY InventoryItemID)
)
UPDATE a
SET TemporaryStock = b.Total + c.Total2
FROM InventoryMaster a
FULL JOIN cte b ON a.InventoryItemID = b.InventoryItemID
FULL JOIN cte2 c ON a.InventoryItemID = c.InventoryItemID
My query problem is in point TemporaryStock = b.Total + c.Total2
If one from two tables doesnt have any result it doesnt update my stock in specific item.
Should I add this two tables inside the same cte? Without using Join the second table?
Take a look at this screenshot: