Symmetric Pairs

Sort by

recency

|

1501 Discussions

|

  • + 0 comments
    WITH data1 AS(
    SELECT f.x x1, f.y y1, ROW_NUMBER() OVER(ORDER BY f.x) RN1
    FROM functions f
    ),
    
    data2 AS(
    SELECT f.x x2, f.y y2, ROW_NUMBER() OVER(ORDER BY f.x) RN2
    FROM functions f
    )
    
    SELECT DISTINCT x1, y1
    FROM data1
    JOIN data2
        ON x1=y2 AND y1=x2 AND RN1 != RN2 AND x1 <= y1
    ORDER BY x1
    
  • + 0 comments

    Select sth.X,sth.Y from ( select f1.X as X, f1.Y AS Y --, f2.X as X2,f2.Y as Y2 from functions f1 CROSS JOIN functions f2 where f1.X< f1.Y and f1.X = f2.Y and f2.X = f1.Y

    UNION

    SELECT X, Y from functions where X = Y group by X,Y having count(*) > 1) sth Order by sth.X,sth.Y

  • + 1 comment
    WITH NUM AS
        (SELECT ROW_NUMBER() OVER(ORDER BY X,Y) RN, X,Y
        FROM FUNCTIONS)
    
    SELECT F1.X,F1.Y
    FROM NUM F1
    INNER JOIN NUM F2
    ON F1.X =F2.Y AND F1.Y = F2.X AND F1.RN < F2.RN
    ORDER BY F1.X
    
    • + 0 comments

      thank you

  • + 0 comments

    Assign row numbers using ROW_NUMBER() to prevent self-joins and ensure each row is uniquely identifiable.

    The join condition finds symmetric pairs (X1 = Y2, X2 = Y1), and f1.rn < f2.rn avoids joining a row with itself and also make sures that X1 < X2 and Y1 > Y2.

    WITH RankedRows AS (SELECT ROW_NUMBER() OVER (ORDER BY X, Y) AS rn, X, Y FROM Functions),
    Pairs AS (SELECT f1.X AS X1, f1.Y AS Y1, f2.X AS X2, f2.Y AS Y2  
              FROM RankedRows f1 JOIN RankedRows f2 
              ON f1.X = f2.Y AND f2.X = f1.Y AND f1.rn < f2.rn)
    SELECT X1, Y1 FROM Pairs ORDER BY X1;
    
  • + 0 comments
    WITH _cte_functions_rules AS (
        SELECT * FROM (
            SELECT x, y, count(1) AS xy_count FROM Functions GROUP BY x, y
        ) a WHERE x <> y OR xy_count > 1
    )
    SELECT least(f1.x, f1.y) AS x, greatest(f1.y, f1.x) AS f
      FROM _cte_functions_rules f1
      JOIN _cte_functions_rules f2
        ON f2.y = f1.x
       AND f2.x = f1.y
     GROUP BY 1,2
     ORDER BY x