I am trying to write a query that will return records where the field in column 1 is the same, but there is at least one difference in specific other columns.
For example:
I want to compare columns 2 and 4 for each value in column 1. If there is a difference, I need all fields in both rows to return.
This data set
Column1|Column2|Column3|Column4|Column5
Air Filter|Shape|Round|Color|Red
Air Filter|Shape|Panel
Nerf Bar|Finish|Polished|Material|Stainless Steel
Nerf Bar|Finish|Powder Coated|Material|Stainless Steel
Hood|Color|Black|Finish|Powder Coated
Hood|Finish|Powder Coated|Color|Black
would return:
Air Filter|Shape|Round|Color|Red
Air Filter|Shape|Panel|NULL|NULL
because the value in column 4 does not match between the records
And
Hood|Color|Black|Finish|Powder Coated
Hood|Finish|Powder Coated|Color|Black
because the values in column 2 and column 4 do not match between the records
I can only get one record for each part to return.
Here is the code that I have tried:
Select *
from [My_Table] as a
where exists(select null from [My_Table] as b
where a.column1 = b.column1
and (a.column2 = b.column2
or a.column3 = b.column3
or a.column4 = b.column4
or a.column5 = b.column5))
1 Answer