SQL Union 2 tables, how to only return row from T1 if duplicate is found in T2, else show T2 rows

StackOverflow https://stackoverflow.com/questions/22107225

  •  18-10-2022
  •  | 
  •  

Question

I'm having some trouble with a query in Access.

Current query:

SELECT 'Stock' as availablefrom, PartName, UnitPrice, Description, PartID, PartNumber
FROM PartsOnHand
UNION
SELECT 'Supplier', PartName, UnitPrice, Description, PartID, PartNumber
FROM Parts
ORDER BY PartName, PartID;

Which returns:

Availablefrom, PartName, PartID.. etc
Stock, Air Filter, 1
Supplier, Air Filter, 1

When I only want it to return the first result, from stock and ignore the second one, from supplier. Duplicates will only be present in the PartsOnHand table. all columns in the table are the same, except for the 'availablefrom' column made in this query

Cheers

No correct solution

OTHER TIPS

Maybe in the bottom part throw in a left join to T1 on part id where T1's part Id is null

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top