Skip to content Skip to sidebar Skip to footer

Multiple Joins With Same Column Sql Server

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 +---

Solution 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

Post a Comment for "Multiple Joins With Same Column Sql Server"