Symmetric Pairs

  • + 0 comments

    The part where people get it wrong is that only one record of 1,1 will not generate a symmetric pair. There has two be repeating lines of 1,1 and 1,1. In the dataset there is only 13, 13 and 13, 13 that fullfills this. you have to treat non identical records and repeating identical records in separate queries.

    SELECT DISTINCT non_duplicates.X, non_duplicates.Y FROM ( SELECT X, Y FROM FUNCTIONS WHERE X != Y ) AS non_duplicates JOIN FUNCTIONS AS F2 ON non_duplicates.X = F2.Y WHERE non_duplicates.Y = F2.X AND non_duplicates.X <= non_duplicates.Y

    UNION

    SELECT X, Y FROM FUNCTIONS WHERE X = Y GROUP BY X, Y HAVING COUNT(*) > 1

    ORDER BY X ASC;