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
|
1555 Discussions
|
Please Login in order to post a comment
My solution:
with cte1 as (select x, y, row_number() over( order by x) as ro_num from Functions), cte2 as (select x, y from cte1 oq where y in (select x from cte1 where ro_num != oq.ro_num and y = oq.x)) select top((select count(*) from cte2)/2) x, y from cte2 order by x;
WITH CTE AS ( SELECT x,y,(x+y) AS sm, abs(x-y) AS sb FROM functions ), CTE2 AS( SELECT *, row_number() OVER(PARTITION BY sm, sb ORDER BY x) AS rnk FROM CTE) SELECT y,x FROM cte2 WHERE rnk = 2 ORDER BY y;
select distinct a.x, a.y from Functions a inner join functions b on a.x=b.y and b.x=a.y where a.x<=a.y
-- to remove self join cases if equal pair (A,A) exist in the table, it will self join so i'll check the result in the base table that (A,A) cases should have atleast 1 entry
and (a.x != a.y or ( select count(*) from functions where x=a.x and y=a.y )>1) order by a.x
MS SQL Server