Removing Mirrored Pairs from SQL Join

前端 未结 3 589
遥遥无期
遥遥无期 2021-02-10 19:40

I have a table with 2 fields (name, interest) and I want to find all pairs that have the same interest, with all duplicates and mirrored pairs removed.

I am able to find

3条回答
  •  春和景丽
    2021-02-10 20:04

    Assuming you do not care which pair ends up sticking around (ben,will) vs (will, ben), then my preferred solution is to do the following:

    DELETE p2 
    FROM Pairs p1 
    INNER JOIN Pairs p2 
        on p1.Name1 = p2.Name2 
        and p1.Name2 = p2.Name1 
        and p1.Interest = p2.Interest
        -- match only one of the two pairs
        and p1.Name1 > p1.Name2
    

    By virtue of the fact that you would never have Name1 and Name2 equal, there must always be one pair where the first member is less than the second member. Using that relationship, we can delete the duplicate.

    This is especially trivial if you have a surrogate key for the relationship, as then the requirement for Name1 and Name2 to be unequal goes away.

    Edit: if you don't want to remove them from the table, but just from the results of a specific query, use the same pattern with SELECT rather than DELETE.

提交回复
热议问题