I need to find matched pairs of records in SQL Server, but each record can only be included in 1 pair. Once a record has been matched with a pair, it should be removed from consideration for any future pairs.
I have tried solutions involving ROW_NUMBER()
and LEAD()
, but i just can't quite get there.
This will be used to pair financial accounts with similar accounts for review, based on multiple customer attributes such as credit score, income, etc.
Statement:
declare @test table (ID numeric, Color varchar(20))
insert into @test values
(1,'Blue'),(2,'Red'),(3,'Blue'),(4,'Yellow'),(5,'Blue'),(6,'Red')
select*
from @test t1
join @test t2
on t1.Color = t2.Color
and t1.ID < t2.ID -----removes reverse-pairs and self-pairs
Current results:
ID Color ID Color
--- ------- --- --------
1 Blue 3 Blue
1 Blue 5 Blue -----should not appear because 1 has already been paired
3 Blue 5 Blue -----should not appear because 3 and 5 have already been paired
2 Red 6 Red
Needed results:
ID Color ID Color
--- ------- --- --------
1 Blue 3 Blue
2 Red 6 Red