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
|
1435 Discussions
|
Please Login in order to post a comment
with f1_temp as ( select x, y, count(*) as cnt from functions where x <= y group by x , y )
, f2_temp as( select x, y from functions where x > y )
select f1.x, f1.y from f1_temp f1 where f1.cnt >1 or ( f1.x in (select f2.y from f2_temp f2 ) and f1.y in (select f2.x from f2_temp f2 ) ) order by f1.x asc
Without using CTEs:-
SELECT X, Y FROM ( (SELECT FN1.X AS X, FN1.Y AS Y
FROM FUNCTIONS FN1 JOIN FUNCTIONS FN2 ON FN1.X = FN2.Y AND FN1.Y = FN2.X
AND FN1.X <> FN1.Y AND FN1.X <= FN1.Y ) UNION (SELECT FN3.X AS X, FN3.Y AS Y FROM FUNCTIONS FN3 WHERE FN3.X = FN3.Y GROUP BY FN3.X, FN3.Y HAVING COUNT(*) > 1 ) ) ALIAS ORDER BY X
ngoccth_SQL SERVER: WITH table_1 AS ( SELECT F1.X As X, F1.Y as Y, COUNT(F1.X) as num FROM Functions AS F1 INNER JOIN Functions AS F2 ON F1.X = F2.Y AND F1.Y = F2.X AND F1.X <= F1.Y AND F2.X >= F2.Y GROUP BY F1.X, F1.Y ) SELECT X, Y from table_1 WHERE X != Y OR (X = Y AND num > 1) ORDER BY X
WITH CTE1 AS( SELECT X,Y,ROW_NUMBER() OVER(ORDER BY X ) As row_n FROM Functions )
SELECT DISTINCT X,Y FROM( SELECT a.X,a.Y FROM CTE1 AS a INNER JOIN CTE1 AS b On a.X=b.Y AND a.Y=b.X AND a.row_n!=b.row_n) t1 WHERE X<=Y