I'm trying to calculate number of cluster when I know all members of each cluster.
I need a function in SqlServer2008 to solve this problem.
My table has more than 14 million distinct records like this:
CREATE TABLE Test
(
F1 varchar(5),
F2 varchar(5)
)
INSERT INTO TEST ( F1, F2) VALUES ( 'A', 'B')
INSERT INTO TEST ( F1, F2 ) VALUES ( 'A', 'K')
INSERT INTO TEST ( F1, F2) VALUES ( 'C', 'H')
INSERT INTO TEST ( F1, F2 ) VALUES ( 'D', 'B')
INSERT INTO TEST ( F1, F2 ) VALUES ( 'F', 'I')
INSERT INTO TEST ( F1, F2 ) VALUES ( 'F', 'B')
INSERT INTO TEST ( F1, F2 ) VALUES ( 'D', 'H' )
INSERT INTO TEST ( F1, F2 ) VALUES ( 'E', 'G' )
INSERT INTO TEST ( F1, F2 ) VALUES ( 'G', 'L' )
note that:
If A=B AND A=K THEN B=K
IF E=G AND G=L THEN E=L
Now the output of the function should be as below:
ClusterNumber--- point
1---A
1---B
1---k
1---D
1---F
1---I
1---H
1---C
2---E
2---G
2---L
I guess the solution would be a recursive function, but does not know it!