The table contains PID and corresponding values. PIDs with same values should be assigned same group id. A, B, D are directly connected (value = ABC) and since C is connected with D (value = GHI), A,B,C & D are all part of the same group.
PID Value
--------------
A101 ABC
A101 XYZ
B101 ABC
C101 GHI
C101 DEF
D101 ABC
D101 GHI
E101 KLM
F101 MNO
Expected Result:
PID GroupID
--------------
A101 1
B101 1
C101 1
D101 1
E101 2
F101 3
I have tried below query without success:
with cte as (
select PID, Value, Rank() OVER(ORDER BY Value) rn
from t
union all
select t.PID, cte.Value, cte.rn
from t join cte
on t.Value = cte.Value and
t.PID < cte.PID
)
select *
from cte
Is there a way to fix the query and also write a more efficient query (about 1 million rows) in SQL Server 2012/2014?