I am trying to import a number of items into this system. Many of the items exist, but they need additional information added to them. I'm trying to exclude them if they already exist.
Here are two of the tables with sample data, notice the NULL in the vw_suppliers
Focus Item
-- inv_mast_uid, -- item_id, -- new_inv_mast_uid
1234, Widget 1, 4321
2345, Widget 2, 5432
vw_Suppliers
-- supplier_id, -- new_supplier_id, -- division
987, 789, 789
876, NULL, NULL
765, 567, 567
Here is my SQL:
SELECT
fi.item_id,
fi.new_inv_mast_uid,
bhs.new_supplier_id
bhs.new_division_id
FROM Focus_Item fi
LEFT JOIN SourceSQL.dbo.inv_loc il
ON il.inv_mast_uid = fi.inv_mast_uid
LEFT JOIN DC_SQL.dbo.vw_suppliers bhs
ON bhs.legacy_id = il.primary_supplier_id
LEFT JOIN TargetSQL.dbo.inventory_supplier bh_iss
ON bh_iss.inv_mast_uid = fi.bh_inv_mast_uid
AND bh_iss.supplier_id = bhs.bh_supplier_id
AND bh_iss.division_id = bhs.division_id
AND bh_iss.delete_flag = 'N'
WHERE
bhs.bh_supplier_id IS NOT NULL
AND bh_iss.supplier_id IS NULL
AND bh_iss.division_id IS NULL
So the inv_mast_uid, supplier_id and division need to be unique in the inventory_supplier table. If it already exists in the inventory_supplier table, then I want to exclude it from the list. I THOUGHT a LEFT JOIN would give me NULLs if it wasn't found, then I could exclude them with the IS NULL in the WHERE clause.
Items are in multiple locations, and locations can have multiple suppliers.
The above result seems to give me a MUCH smaller value than expecting. All inclusive I get about 1200 results. I'm expecting to remove 300-400 once I exclude the dupes, but I get like 300 results instead.
Is there a better way to exclude the dupes?