Hi Seem to be getting duplicates from this sql code it suppose to find records on BO Data that is not on Order Data by using the following as a key Keyorderstatus all my query results are on the Order Data already, How can a remedy this issue?
x
SELECT Keyorderstatus,
OrderNumber,
PartsNo,
HoldType,
ShiptoCode,
BackOrderQty,
OrderStatusCode
FROM [BO Data]
WHERE NOT EXISTS (SELECT *
FROM [BO Data]
WHERE [BO Data].Keyorderstatus = [Order Data].Keyorderstatus
)
AND [BO Data].OrderStatusCode="AWAITING_SHIPPING"
AND NOT EXISTS (SELECT *
FROM [Order Data]
WHERE Keyorderstatus IS NULL);
Advertisement
Answer
As @dfundako said – hard to check without data tables to test against.
I’m thinking this SQL should do what you’re after:
SELECT [BO Data].KeyOrderStatus
, OrderNumber
, PartsNo
, HoldType
, ShipToCode
, BackOrderQty
, [BO Data].OrderStatusCode
FROM [BO Data] LEFT JOIN [Order Data] ON [BO Data].KeyOrderStatus <> [Order Data].KeyOrderStatus
WHERE [BO Data].OrderStatusCode = 'AWAITING SHIPPING'
GROUP BY [BO Data].KeyOrderStatus
, [BO Data].OrderStatusCode
Checking [Order Data].KeyOrderStatus IS NULL
may not be necessary as (I’m assuming) [BO Data].KeyOrderStatus
is a Primary Key so it must always contain a value and the SQL checks if that’s different from the value in [Order Data] – if it’s NULL it will be different from the PK.
Edit: I’ve added in the other fields not relevant to the joins. You’ll have to group on these to.