Find duplicate item based on related reference

Hi,
I have an item table where the ID matches either the first column or the second column of the relations table. If the item is found in the relations table in column 1 then I need to check if in the item table I find a row with the same id as the second column or vica versa.
how can I achieve the following result in TSQL

image


SELECT 
    r.Item_C,
    CASE WHEN EXISTS(SELECT * FROM items i2 WHERE r.item_N = i2.ID) 
         THEN 'Y' ELSE 'N' END AS duplicate_found
FROM relations r
INNER JOIN items i ON r.item_C = i.ID
1 Like

Thank you very much for this quick and working response :+1:

1 Like

And thank you for the feedback, it's appreciated.