Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a mapping table in sql with columns Col1 and Col2.The criteria for retrieving the data is Col1 value. For eg: if the Col1 value =6, I should get all the related Col2 data from the table.
6 is mapped to the col2 data 16,20,2,23.Now all these values should be in turn looked up and all the corresponding mappings should be retrieved.
So For 16, it has mapping to 13,15,8 in Col1.Now these values should be looked up and its mappings should be retrieved.
So for Col1 which has value 6, I need to get all the nested values from Col2

Col1 Col2
6 16
6 20
6 2
6 23
8 24
8 4
8 21
8 2
8 16
8 1
13 21
13 22
13 23
13 16
13 2
13 18
15 2
15 16
15 24
15 4
16 35

What I have tried:

I tried CTE in sql server but could not find a solution.
Posted
Updated 7-Sep-17 17:43pm
Comments
David_Wimbley 7-Sep-17 13:42pm    
Are you able to change your schema? If i understand you correctly you are trying to create a many to many relationship in the same table which gets funky. If not.

Do you need to get the forward/backward relationship of all columns in 1 query by a selected Id? Because if you need to get all relationships it would just be a SELECT * FROM <table> i imagine.
Member 3521466 7-Sep-17 14:09pm    
Thanks David for your reply. I am trying to find the many to many relationship in the same table. For e.g.: if the input is Col1=6 I should get the output as col2 values 1,2,4,16,18,20,21,22,23,24(I did this manually by checking the relationships.)
If the input is col1=16 then the output should be col2 value 35.
Can you please advise?

So i think you aught to change your schema so this query isn't the most ideal way of going about this im sure. I can think of a way of doing this while looping over each row and storing the results which would be slower than this also.

Basically it uses a join and a sub query to get the column1 + column2 matching results.

I get the same results as OP's original question but I don't get the same results as specified in the comment to my question. Manually doing the relationship i don't see how 18 can be a possible match for example.

SQL
DECLARE @Relationship TABLE (
	Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
	Column1 INT NULL,
	Column2 INT NULL
);

INSERT INTO @Relationship ( Column1, Column2 ) VALUES (6,16);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (6,20);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (6,2);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (6,23);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,24);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,4);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,21);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,2);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,16);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (8,1);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,21);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,22);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,23);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,16);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,2);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (13,18);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (15,2);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (15,16);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (15,24);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (15,4);
INSERT INTO @Relationship ( Column1, Column2 ) VALUES (16,35);


DECLARE @Input INT = 6

SELECT DISTINCT A.Column2 AS Result1 FROM @Relationship AS A
LEFT OUTER JOIN @Relationship AS B ON B.Column1 = A.Column2
WHERE A.Column1 = @Input 

UNION ALL

SELECT DISTINCT  A.Column1 AS Result1 FROM @Relationship AS A 
WHERE A.Column2 IN (SELECT DISTINCT A.Column2 FROM @Relationship AS A
LEFT OUTER JOIN @Relationship AS B ON B.Column1 = A.Column2
WHERE A.Column1 = @Input ) AND A.Column1 <> @Input


The above gives me the output of

Quote:
2
16
20
23
8
13
15
 
Share this answer
 
Comments
Member 3521466 8-Sep-17 8:05am    
Hi David,
Thanks a lot for spending your time on this. I think I confused you with values in Column 1 and Column2.
The values in these two columns are two different values. S o I have replaced the values of Column 2 with alphabets.

Column1 Column2
6 P
6 T
6 B
6 W
8 X
8 D
8 U
8 B
8 P
8 A
10 Q
11 Q
13 U
13 V
13 W
13 P
13 B
13 R
15 B
15 P
15 X
15 D
16 Z


When the input is 6, I first get all the mapping values from Column2 which are P,T,B,W.
Now I need to look again in the table for mappings for P,T,B,W and I get the mapping s for these values in Column1 as
P maps to 6,8,13,15
T maps to 6
B maps to 6,8,13,15
W maps to 6,13
All together it would be 6,8,13,15.
The mappings for these values in Col 2 are A,B,D,P,R,T,U,V,W,X. Now the mappings for these Col2 values should be figured out
A ->8
B ->6,8,13,15
D ->8,15
P ->6,8,13,15
R ->13
T ->6
U-> 8,13
V ->13
W ->6,13
X ->8,13,15

So all together it would be col1 values 6,8,13,15 for which we have already figured out the mappings in Col2.

So the final result will be Col2 values A,B,D,P,R,T,U,V,W,X for the input Col1 value 6.

If the input Col1 value is 16 the output will be the result Col2 value Z since it has no other mappings.

If SQL query is going to be hard then C# also can be used along with sql. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900