Ok, so I have an intermediate table for mapping suppliers from one system to another. There are a bunch of instances where they want to map multiple suppliers from the old system to the new system. So my map table looks like
legacy new
12345 1
12346 1
54321 2
9876 3
9875 3
When I join the product table to the supplier table for the old system it's pulling multiple suppliers (12345, 12346). So when I join to the map table to get the new id, I'm getting a duplicate row since it's joining both legacy suppliers to the same new one.
There are cases where a product has multiple suppliers (12346, 54321) that map to diff new ids, which is good/valid.
Assume the product IDs are the same between systems, code looks something like this.
SELECT *
FROM products p
INNER JOIN suppliers s -- Find the supplier(s) for the product
ON s.pid = p.pid
LEFT JOIN supplier_map sm -- Get the new supplier IDs
ON sm.legacy_id = s.sid
LEFT JOIN NEWSYS.dbo.product_supplier ps -- Checks to see if item/supplier combo already exists in new system
ON ps.item_id = p.pid
AND ps.supplier_id = sm.new_id
WHERE ps.item_id IS NULL -- We only want new item/supplier combos
So how can I construct the join so that I only get one result if the new_id is the same for the same product (ie how do I eliminate the dupe?)
Running on MS SQL Server 13.0.4001.0