在SQL Server中的同一个表中合并双向关系

时间:2014-11-27 10:40:17

标签: sql sql-server

当前数据

ID      | Name1      | Name2
<guid1> | XMind      | MindNode    
<guid2> | MindNode   | XMind         
<guid3> | avast      | Hitman Pro    
<guid4> | Hitman Pro | avast
<guid5> | PPLive     | Hola!    
<guid6> | ZenMate    | Hola!    
<guid7> | Hola!      | PPLive
<guid8> | Hola!      | ZenMate  

所需输出

ID1       | ID2       | Name1  | Name2
<guid1>   | <guid2>   | XMind  | MindNode 
<guid3>   | <guid4>   | avast  | Hitman Pro  
<guid5>   | <guid7>   | PPLive | Hola!
<guid6>   | <guid8>   | Hola!  | ZenMate         

这些是应用之间的关系。我想表明阿瓦斯特和希特曼有一种关系,但在这种观点中,我不需要在什么方向上显示&#34;方向&#34;他们有关系。在这种观点中,这种关系是双向的。

编辑:好像我的例子很简单。该解决方案无法处理更多数据。

DECLARE @a TABLE (ID INT, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT  INTO @a VALUES  ( 1, 'XMind', 'MindNode' )    
INSERT  INTO @a VALUES  ( 2, 'MindNode', 'XMind' )       
INSERT  INTO @a VALUES  ( 3, 'avast', 'Hitman Pro' )  
INSERT  INTO @a VALUES  ( 4, 'Hitman Pro', 'avast' )
INSERT  INTO @a VALUES  ( 5, 'PPLive Video Accelerator', 'Hola! Better Internet' )    
INSERT  INTO @a VALUES  ( 6, 'ZenMate', 'Hola! Better Internet' )       
INSERT  INTO @a VALUES  ( 7, 'Hola! Better Internet', 'PPLive Video Accelerator' )  
INSERT  INTO @a VALUES  ( 8, 'Hola! Better Internet', 'ZenMate' )

SELECT  a1.ID AS ID1 ,
        a2.ID AS ID2 ,
        a1.Name1 ,
        a2.Name1 AS Name2
FROM    @a a1
JOIN @a a2 ON a1.Name1 = a2.Name2
        AND a1.ID < a2.ID -- avoid duplicates

然而,这是有效的,所以我猜是Guid正在弄乱我。

再次编辑:

我有一段时间没看过这个,我认为它有效,但我才意识到它没有。我整个上午都在苦苦挣扎,但我必须承认SQL并不是我的强大套件。事情是这样的。

DECLARE @a TABLE (ID int, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT  INTO @a VALUES  ( 1, 'XMind', 'MindNode' )    
INSERT  INTO @a VALUES  ( 2, 'MindNode', 'XMind' )       
INSERT  INTO @a VALUES  ( 3, 'avast', 'Hitman Pro' )  
INSERT  INTO @a VALUES  ( 4, 'PPLive Video Accelerator', 'Hola! Better Internet' )    
INSERT  INTO @a VALUES  ( 5, 'ZenMate', 'Hola! Better Internet' )
INSERT  INTO @a VALUES  ( 6, 'Hitman Pro', 'avast' )       
INSERT  INTO @a VALUES  ( 7, 'Hola! Better Internet', 'PPLive Video Accelerator' )  
INSERT  INTO @a VALUES  ( 8, 'Hola! Better Internet', 'ZenMate' )
INSERT  INTO @a VALUES  ( 9, 'XX', 'A' )  
INSERT  INTO @a VALUES  ( 10, 'XX', 'BB' )  
INSERT  INTO @a VALUES  ( 11, 'BB', 'XX' )
INSERT  INTO @a VALUES  ( 12, 'A', 'XX' )
INSERT  INTO @a VALUES  ( 13, 'XX', 'CC' )  
INSERT  INTO @a VALUES  ( 14, 'CC', 'XX' )

;With CTE as
(
SELECT  a1.ID AS ID1 ,
        a2.ID AS ID2 ,
        a1.Name1 ,
        a2.Name1 AS Name2,
        CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end) ck, -- just for display
        Row_Number() over (Partition by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)
                           order by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)) as rn

FROM    @a a1
JOIN @a a2 ON a1.Name1 = a2.Name2
)
Select ID1, ID2,Name1, Name2
from CTE C1
where rn=1

当我使用此代码时,它确实可以正常使用名称,但它与ID无法正确匹配。

结果是

ID1       | ID2       | Name1  | Name2
12        | 9         | A      | X (Correct)
7         | 5         | Hola!  | ZenMate (Not Correct)   
[..]

我整个早上都拔了头发,但我无法弄清楚这一点。我仍然使用Guid作为ID,只是在这里使用Int来使它更​​具可读性。

6 个答案:

答案 0 :(得分:7)

DECLARE @a TABLE (ID INT, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT  INTO @a VALUES  ( 1, 'XMind', 'MindNode' )    
INSERT  INTO @a VALUES  ( 2, 'MindNode', 'XMind' )       
INSERT  INTO @a VALUES  ( 3, 'avast', 'Hitman Pro' )  
INSERT  INTO @a VALUES  ( 4, 'Hitman Pro', 'avast' )

SELECT  a1.ID AS ID1 ,
        a2.ID AS ID2 ,
        a1.Name1 ,
        a2.Name1 AS Name2
FROM    @a a1
JOIN @a a2 ON a1.Name1 = a2.Name2
        AND a1.ID < a2.ID -- avoid duplicates

参考您的问题的修改和扩展,需要一个更复杂的解决方案。 我们在a1.Name1,a2.Name上形成CHECKSUM(为了得到我们在大小上交换的相同内容) 使用此方法,我们使用ROW_NUMBER (Transact-SQL)生成一个数字,并仅使用数字为1的结果中的行。

DECLARE @a TABLE (ID uniqueIdentifier, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT  INTO @a VALUES  ( NewID(), 'XMind', 'MindNode' )    
INSERT  INTO @a VALUES  ( NewID(), 'MindNode', 'XMind' )       
INSERT  INTO @a VALUES  ( NewID(), 'avast', 'Hitman Pro' )  
INSERT  INTO @a VALUES  ( NewID(), 'Hitman Pro', 'avast' )
INSERT  INTO @a VALUES  ( NewID(), 'PPLive Video Accelerator', 'Hola! Better Internet' )    
INSERT  INTO @a VALUES  ( NewID(), 'ZenMate', 'Hola! Better Internet' )       
INSERT  INTO @a VALUES  ( NewID(), 'Hola! Better Internet', 'PPLive Video Accelerator' )  
INSERT  INTO @a VALUES  ( NewID(), 'Hola! Better Internet', 'ZenMate' )
INSERT  INTO @a VALUES  ( NewID(), 'XX', 'A' )  
INSERT  INTO @a VALUES  ( NewID(), 'A', 'XX' )
INSERT  INTO @a VALUES  ( NewID(), 'XX', 'BB' )  
INSERT  INTO @a VALUES  ( NewID(), 'BB', 'XX' )
INSERT  INTO @a VALUES  ( NewID(), 'XX', 'CC' )  
INSERT  INTO @a VALUES  ( NewID(), 'CC', 'XX' )


;With CTE as
(
SELECT  a1.ID AS ID1 ,
        a2.ID AS ID2 ,
        a1.Name1 ,
        a2.Name1 AS Name2,
        CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end) ck, -- just for display
        Row_Number() over (Partition by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)
                           order by CheckSum(Case when a1.Name1>a2.Name1 then a2.Name1+a1.Name1 else a1.Name1+a2.Name1 end)) as rn

FROM    @a a1
JOIN @a a2 ON a1.Name1 = a2.Name2
)
Select *
from CTE C1
where rn=1

编辑:
如果您只想获得两个字段都适合的那些,那么所需的查询就是:

SELECT a1.ID AS ID1 , a2.ID AS ID2 , a1.Name1 , a2.Name1 AS Name2 
FROM @a a1 
JOIN @a a2 ON a1.Name1 = a2.Name2 and a1.Name2 = a2.Name1 AND a1.ID < a2.ID

答案 1 :(得分:4)

如果输出应仅包含双向关系('XX' + 'A') AND ('A' + 'XX'),请尝试:

;
WITH m    (ID1, ID2, Name1, Name2) AS (
    SELECT ID1, ID2, Name1, Name2
    FROM (
        SELECT a1.ID AS ID1
              ,a2.ID AS ID2
              ,a1.Name1 AS Name1
              ,a2.Name1 AS Name2
              ,ROW_NUMBER() OVER (PARTITION BY a1.Name1, a2.Name1 ORDER BY (SELECT 1)) AS n
        FROM @a AS a1
        JOIN @a AS a2
            ON a1.Name1 = a2.Name2
                AND a1.Name2 = a2.Name1
    ) AS T
    WHERE n = 1
)
SELECT DISTINCT *
FROM (
    SELECT ID1, ID2, Name1, Name2
    FROM m
    WHERE ID1 <= ID2

    UNION ALL

    SELECT ID2, ID1, Name2, Name1
    FROM m
    WHERE ID1 > ID2
) AS dm

它产生如下输出:

+------+-----+--------------------------+-----------------------+
| ID1  | ID2 | Name1                    | Name2                 |
+------+-----+--------------------------+-----------------------+
|    1 |   2 | XMind                    | MindNode              |
|    3 |   6 | avast                    | Hitman Pro            |
|    4 |   7 | PPLive Video Accelerator | Hola! Better Internet |
|    5 |   8 | ZenMate                  | Hola! Better Internet |
|    9 |  12 | XX                       | A                     |
|   10 |  11 | XX                       | BB                    |
|   13 |  14 | XX                       | CC                    |
+------+-----+--------------------------+-----------------------+

答案 2 :(得分:3)

只需使用ROW_NUMBER函数对您的行进行排名,并在join而非原始ID列中使用此排名:

DECLARE @a TABLE (ID UNIQUEIDENTIFIER, Name1 VARCHAR(50), Name2 VARCHAR(50))
INSERT  INTO @a VALUES  ( NEWID(), 'XMind', 'MindNode' )    
INSERT  INTO @a VALUES  ( NEWID(), 'MindNode', 'XMind' )       
INSERT  INTO @a VALUES  ( NEWID(), 'avast', 'Hitman Pro' )  
INSERT  INTO @a VALUES  ( NEWID(), 'Hitman Pro', 'avast' )
INSERT  INTO @a VALUES  ( NEWID(), 'PPLive Video Accelerator', 'Hola! Better Internet' )
INSERT  INTO @a VALUES  ( NEWID(), 'ZenMate', 'Hola! Better Internet' )       
INSERT  INTO @a VALUES  ( NEWID(), 'Hola! Better Internet', 'PPLive Video Accelerator' )
INSERT  INTO @a VALUES  ( NEWID(), 'Hola! Better Internet', 'ZenMate' )

;WITH cte AS(SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rn FROM @a)
SELECT  a1.ID AS ID1 ,
        a2.ID AS ID2 ,
        a1.Name1 ,
        a2.Name1 AS Name2
FROM    cte a1
JOIN cte a2 ON a1.Name1 = a2.Name2 AND 
               a2.Name1 = a1.Name2 AND 
               a1.rn < a2.rn

输出:

ID1     ID2     Name1                       Name2
Guid    Guid    XMind                       MindNode
Guid    Guid    avast                       Hitman Pro
Guid    Guid    PPLive Video Accelerator    Hola! Better Internet
Guid    Guid    ZenMate                     Hola! Better Internet

答案 3 :(得分:2)

我建议你用这个简单的方法:

SELECT 
    t2.ID, t3.ID ID2,
    t1.Name1,t1.Name2
FROM (
    SELECT DISTINCT
        CASE WHEN Name1 <= Name2 THEN Name1 ELSE Name2 END AS Name1,
        CASE WHEN Name1 <= Name2 THEN Name2 ELSE Name1 END AS Name2
    FROM 
        @a) t1
    JOIN
    @a t2 ON t1.Name1+t1.Name2 = t2.Name1+t2.Name2
    JOIN
    @a t3 ON t1.Name1+t1.Name2 = t3.Name2+t3.Name1

为此:

ID  | ID2 | Name1                 | Name2
----+-----+-----------------------+---------------------------
12  | 9   | A                     | XX
3   | 4   | avast                 | Hitman Pro
11  | 10  | BB                    | XX
14  | 13  | CC                    | XX
7   | 5   | Hola! Better Internet | PPLive Video Accelerator
8   | 6   | Hola! Better Internet | ZenMate
2   | 1   | MindNode              | XMind

答案 4 :(得分:2)

您可以使用CROSS APPLY

解决此问题
SELECT a2.ID ID_1,a1.ID ID_2, a2.Name1 , a2.Name2
FROM @a a1
CROSS APPLY
(
    SELECT ID, Name2, Name1 
    FROM @a aa
    WHERE aa.Name1 = a1.Name2 AND a1.Name1 = aa.Name2 AND a1.ID > aa.ID
) a2

答案 5 :(得分:2)

您也可以尝试:

select min(ID) ID1,
       max(ID) ID2,
       Name1,
       Name2
from ( -- Here I get all the IDs and each couple sorted 
       -- Change > to < if you don't like the order
       select ID, 
              case
                  when Name1 > Name2 then Name1
                  else Name2
              end Name1,
              case
                  when Name1 > Name2 then Name2
                  else Name1
              end Name2
       from table1
      ) as t
group by Name1, 
         Name2

你甚至可以在没有内部查询的情况下在一个simgle查询中形成这个,但我认为这样更可读,你可以更好地理解我的方法。