我有一个sql查询,它应该根据某些不匹配的值返回记录。在我的BaseStock表中,我有了realQty(int)和baseQty(int)。在另一个表中,我得到了ordersQty(根据actualQty和baseQty得出)。 orderQty将是baseQty - actualQty。
下订单需要baseQty - actualQty来生成orderQty。但是如果已经下订单并且orderQty + actualQty = baseQty,则不应该带回那些记录。
我所做的查询没有带回任何记录..... 这就是我所做的:
SELECT BS.[ID],BS.[WarehouseNo],BS.[Customer],BS.[Site],BS.[VendorNo],BS.[Description],
BS.[ActualQty],BS.[BaseQty], (BS.[BaseQty] - BS.[ActualQty]) AS PlaceOrderQty
FROM [dbo].[StockControlBaseStock] AS BS
RIGHT JOIN [dbo].[StockControlStockOrder] AS SO
ON BS.ID = SO.BaseStockRef
WHERE (BS.[ActualQty] + SO.OrderQTY) <> BS.[BaseQty]
AND BS.[ActualQty] IS NOT NULL
AND BS.[BaseQty] IS NOT NULL
我做错了吗?任何帮助将不胜感激!!!
答案 0 :(得分:1)
您可以尝试以下解决方案吗?
SELECT BS.[ID],BS.[WarehouseNo],BS.[Customer],BS.[Site],BS.[VendorNo],BS.[Description],
BS.[ActualQty],BS.[BaseQty], (BS.[BaseQty] - BS.[ActualQty]) AS PlaceOrderQty
FROM [dbo].[StockControlBaseStock] AS BS
LEFT JOIN [dbo].[StockControlStockOrder] AS SO
ON BS.ID = SO.BaseStockRef
WHERE (BS.[ActualQty] + Iif(SO.OrderQTY IS NULL, 0, SO.OrderQTY)) <> BS.[BaseQty]
AND BS.[ActualQty] IS NOT NULL
AND BS.[BaseQty] IS NOT NULL
此查询返回所有订单而没有放置足够的订单