简体   繁体   中英

sql query not bringing back correct records

I've got a sql query which should bring back records based on certain values not matching. In my BaseStock table I've got actualQty(int) and baseQty(int). In another table I've got the ordersQty (which is worked out based on the actualQty and baseQty). The orderQty will be baseQty - actualQty.

Placing the order it will take the baseQty - actualQty to make the orderQty. But if the order is already placed and the orderQty + actualQty = baseQty it should not bring back those records.

The query I've made doesn't bring back any records..... This is what I have done:

    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

Is there something I am doing wrong? any help will be greatly appreciated!!!

Could you try the following solution?

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

This query returns all orders without the adequate order placed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM