具有相同列SQL Server的多个JOIN

时间:2016-02-26 11:19:49

标签: sql-server join multiple-columns

我必须从id中选择GraphNodes,而id中的GraphEdges位于两列中,即Source_NodeTarget_Node。表的结构如下:

GraphNodes

+---+---------+-------------------+------------+-----------+
| id | Node_ID | Node             | Node_Label | Node_Type |
+---+---------+-------------------+------------+-----------+
| 1  |  677    | Nuno Vasconcelos | Author     |    1      |
| 2  |  1359   | Peng Shi         | Author     |    1      |
| 3  |  6242   | Z. Q. Shi        | Author     |    1      |
+----+---------+------------------+------------+-----------+    

GraphEdges

+------------+------------------+-------------+------------------+------+-----------+
|Source_Node | Source_Node_Type | Target_Node | Target_Node_Type | Year | Edge_Type |
+------------+------------------+-------------+------------------+------+-----------+
| 1          |  1               |  10965      |     2            | 2005 |   1       |
| 1          |  1               |  10179      |     2            | 2007 |   1       |
| 1          |  1               |  10965      |     2            | 2007 |   1       |
+------------+------------------+-------------+------------------+------+-----------+  

我只为这两个表显示了3行,以便了解表的结构。我使用查询:

SELECT GN.id as Node_ID, 
       COUNT(DISTINCT(CONCAT(GE.Source_Node, '-', GE.Target_Node))) 
       AS Mutual_Links -- OR Node_Degree
FROM GraphEdges GE
JOIN GraphNodes GN ON GN.id = GE.Source_Node --How to JOIN this
                  AND GN.id = GE.Target_Node --How to JOIN this
WHERE (Source_Node IN (SELECT id FROM GraphNodes 
                       WHERE id BETWEEN 1 AND 510) 
                       AND Edge_Type IN (1, 2, 3))
OR (Target_Node IN (SELECT id FROM GraphNodes 
                    WHERE id BETWEEN 1 AND 510) 
                    AND Edge_Type IN (1, 2, 3))
GROUP BY GN.id

我想要输出格式:

+--------+-------------+
|Node_ID | Mutual_Links|
+--------+-------------+
|  1     | 31          |
|  2     | 23          |
|  3     | 12          |
| ...    | ...         |
+--------+-------------+  

问题在于如何将GraphEdges加入GraphNodes我可以idNode_ID表单GraphNodes和COUNT DISTINCT Mutual_Links {{1} }}。

1 个答案:

答案 0 :(得分:0)

这样的事情

;with thedata ( id, MutualLinks ) 
as (Select id, MutualLinks = count(*) from [dbo].[GraphNodes]
inner join [dbo].[GraphEdges] on Source_node = node_id
group by id 
union all
Select id, MutualLinks = count(*)   from [dbo].[GraphNodes]
inner join [dbo].[GraphEdges] on target_node = node_id
group by id )
Select id, total = sum(MutualLinks)
from thedata
group by id