I have the following table with sample data:
Table: tbl_nodes
create table tbl_nodes
(
nod1 varchar(50),
nod2 varchar(50)
);
Sample data:
insert into tbl_nodes values('Node1','Node2');
insert into tbl_nodes values('Node2','Node4');
insert into tbl_nodes values('Node2','Node3');
insert into tbl_nodes values('Node2','Node5');
insert into tbl_nodes values('Node3','Node5');
insert into tbl_nodes values('Node3','Node6');
insert into tbl_nodes values('Node6','Node7');
insert into tbl_nodes values('Node10','Node11');
insert into tbl_nodes values('Node6','Node8');
insert into tbl_nodes values('Node18','Node19');
insert into tbl_nodes values('Node9','Node10');
insert into tbl_nodes values('Node12','Node13');
insert into tbl_nodes values('Node15','Node16');
NOTE: I am having more than 5000 records in the above table.
Expected Result:
------------------------------------
Connectivity
------------------------------------
Node1->Node2->Node3->Node5
Node1->Node2->Node3->Node6->Node7
Node1->Node2->Node3->Node6->Node8
Node1->Node2->Node4
Node1->Node2->Node5
Node9->Node10->Node11
Explaination About expected result: I want to find the connectivity between nodes which are having more than 2 nodes,
for an example Node1
has connectivity with Node2
and Node2
with 3,4,5 and so on as shown in the expected result set.
And want display each connectivity till the end node found, for an example end nodes are Node4
,Node5
,Node7
,Node8
and Node11
.
I tried the following query:
My try:
;WITH CTE AS
(
SELECT nod1,nod2,
CAST(nod1 AS VARCHAR(MAX))+'->' AS conn,
1 as lvl
from tbl_nodes T1
where EXISTS (select 1 from tbl_nodes T2 where T1.nod2 =T2.nod1) OR
EXISTS (select 1 from tbl_nodes T3 WHERE T1.nod1 =T3.nod2)
UNION ALL
SELECT C1.nod1,C1.nod2,
C.conn+CAST(C1.nod1 AS VARCHAR(MAX))+'->',
c.lvl+1
FROM CTE C INNER JOIN tbl_nodes C1 ON C.nod2 = C1.nod1
WHERE CHARINDEX(','+C.nod2+',',C.conn)=0
),cte2 as
(
select * , ROW_NUMBER() over (partition by nod1,nod2 order by lvl)as rn From CTE
),cte3 as
(
select nod1,nod2 ,MAX(LEN(conn)) conn,MAX(rn) rn
from cte2
group by nod1,nod2
)
SELECT DISTINCT c2.conn+c3.nod2 AS Connectivity
from cte3 c3
inner join cte2 c2 on c3.rn = c2.rn and c3.nod1 = c2.nod1
where c3.nod2 not in (select nod1 from cte2)
Above query works fine but unable to get the result for records more than 5000, query keeps running no result.
Edit: I can't attach running data as it has sensitive information, But will explain! I have table with columns Name1
and Name2
which I have referred as Nod1
and Nod2
. I want to find out the relationship between the names like we are finding the link between the nodes here in the given example. The person one (Name1
) may have done some transaction to second person (Name2
) and Name2
may have to do any other person. So I need to find out the link of transactions between the persons. Its just same as the given example. I tried with you given query by partitioning data, for 100 records it comes within seconds, for 500 records it took 1 min and for 5000 records it keeps running because of more permutation and combinations are there. The problem is with last data set (5000) we have to find out the links.