We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Join
- Symmetric Pairs
- Discussions
Symmetric Pairs
Symmetric Pairs
Sort by
recency
|
1542 Discussions
|
Please Login in order to post a comment
This question has wrong description, something that leads to wrong assumptions. One should not use X1 and X2 in example, it means X1 and X2 are adjecent data points. Instead use Xa and Xp where a and p can be any number from 1 to N, N being the size of the data set.
select f1.x, f1.y from functions f1 join functions f2 on f1.x = f2.y and f1.y = f2.x where f1.x < f1.y
UNION
select x,y from functions where x = y group by x,y having count(*) > 1
order by x
This problem can be solve using divide and conquer. There are two cases in this problem: 1:(20,20),(20,20)......... there could be many rows like this 2:(22,23),(23,22)....... there could be many rows like this
if you solve both cases and 'merge' them.
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;