我必须从id
中选择GraphNodes
,而id
中的GraphEdges
位于两列中,即Source_Node
和Target_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
我可以id
或Node_ID
表单GraphNodes
和COUNT DISTINCT Mutual_Links {{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