问题
I have to select id
from GraphNodes
whereas same id
exists in GraphEdges
in two columns i.e. Source_Node
and Target_Node
. The structure of tables are as follows:
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 |
+------------+------------------+-------------+------------------+------+-----------+
I have shown only 3 rows for both tables just to get an idea of table's structures. I have used query as:
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
I want output in the form:
+--------+-------------+
|Node_ID | Mutual_Links|
+--------+-------------+
| 1 | 31 |
| 2 | 23 |
| 3 | 12 |
| ... | ... |
+--------+-------------+
The issue is that how to join GraphEdges
with GraphNodes
that I can get id
OR Node_ID
form GraphNodes
and COUNT DISTINCT Mutual_Links from GraphEdges
.
回答1:
how about something like this
;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
来源:https://stackoverflow.com/questions/35650353/multiple-joins-with-same-column-sql-server