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
|
1453 Discussions
|
Please Login in order to post a comment
SELECT A.X,A.Y FROM Functions A JOIN Functions B ON (A.X = B.Y AND A.Y = B.X) OR (A.X = B.X AND A.Y = B.Y) WHERE A.X <= A.Y GROUP BY A.X,A.Y HAVING COUNT(*) > 1 ORDER BY A.X,A.Y;
Update: Forgot to order the output. It works once I added "order by a.x"...
WITH CTE AS ( select *, row_number() over(order by X asc) as rowN from Functions)
select distinct a.X, a.Y from CTE a inner join CTE b on a.X = b.Y and a.Y = b.X where a.rowN != b.rowN and a.X <= a.Y;
MySQL
WITH SMALLER AS ( SELECT DISTINCT X,Y FROM FUNCTIONS WHERE XY ), SAME AS ( SELECT X,Y, COUNT(*) AS COUNTSAME FROM FUNCTIONS WHERE X=Y GROUP BY X, Y ) SELECT SMALLER.X,SMALLER.Y FROM SMALLER JOIN BIGGER ON (SMALLER.X=BIGGER.Y AND SMALLER.Y=BIGGER.X) UNION ALL SELECT DISTINCT X,Y FROM SAME WHERE MOD(COUNTSAME,2)=0 ORDER BY X