Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I need two table:-
1. TableA

Id|Dimension|
1|abc
2|xyz
3|asd
4|fgh
5|opo

2. TableB

Id|TableAId1|TableAId2|TableAId3|TableAId4
1| 1 |2 |3 |4
2| 1 |5 |3 |4

result should be display:
TableBId|Dimension
1|abc
1|xyz
1|asd
1|fgh
2|abc
2|pop
2|asd
2|fgh

so how to write query for this in sql server?

Please help me.
Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

SELECT ta2.Id,ta1.Dimension
FROM tableA ta1
JOIN tableB ta2 on ta1.id != ta2.id
WHERE ta1.Id = ta2.TableAId1 and ta1.Id = ta2.TableAId2 and ta1.Id = ta2.TableAId3 and ta1.Id=ta2.TableAId3
Posted
Updated 19-Jan-18 1:18am
v2
Comments
[no name] 19-Jan-18 8:22am    
Hey you should add a union to the solution provided in Solution1.The solution 1 doesn't give output 1|abc.
Check my solution at solution 3

SQL
SELECT ta2.Id,ta1.Dimension
FROM Tab1 ta1
JOIN TabB ta2 on ta1.id != ta2.id
WHERE ta1.Id = ta2.tabaid1 or ta1.Id = ta2.tabaid2 or ta1.Id = ta2.tabaid3 or ta1.Id=ta2.tabaid4
 
Share this answer
 
Comments
[no name] 19-Jan-18 8:19am    
Hi,Please vote for my solution.Your solution gives only 7 output.Check my solution below.
Try replacing the AND with OR:
SQL
... WHERE ta1.Id = ta2.TableAId1 OR ta1.Id = ta2.TableAId2 ...
AND requires both sides to be true, OR requires either side to be true, but not necessarily both.
 
Share this answer
 
Comments
[no name] 19-Jan-18 8:15am    
Hi,Please vote for my solution.Your solution gives only 7 output.Check my solution below.
CREATE TABLE TableA(ID int NOT NULL Primary Key,Description varchar(100));
Insert into TableA(ID,Description)values(1,'abc');
Insert into TableA(ID,Description)values(2,'xyz');
Insert into TableA(ID,Description)values(3,'asd');
Insert into TableA(ID,Description)values(4,'fgh');
Insert into TableA(ID,Description)values(5,'opo');
CREATE TABLE TableB([ID] int NOT NULL Primary Key,TableAid1 int NOT NULL ,TableAid2 int NOT NULL,TableAid3 int NOT NULL,TableAid4 int NOT NULL,
                   CONSTRAINT FK_tblBAid1 FOREIGN KEY (TableAid1)
    REFERENCES TableA(ID),CONSTRAINT FK_tblBAid2 FOREIGN KEY (TableAid2)
    REFERENCES TableA(ID),
                   CONSTRAINT FK_tblBAid3 FOREIGN KEY (TableAid3)
    REFERENCES TableA(ID),
                   CONSTRAINT FK_tblBAid4 FOREIGN KEY (TableAid4)
    REFERENCES TableA(ID));
Insert into TableB(ID,TableAid1,TableAid2,TableAid3,TableAid4)values(1,1,2,3,4);
Insert into TableB(ID,TableAid1,TableAid2,TableAid3,TableAid4)values(2,1,5,3,4);

SELECT ta2.Id,ta1.Dimension
FROM tableA ta1
JOIN tableB ta2 on ta1.id != ta2.id
WHERE ta2.TableAId1=ta1.Id OR ta2.TableAId2=ta1.Id  OR ta2.TableAId3=ta1.Id  OR ta2.TableAId3=ta1.Id
UNION
SELECT ta2.ID,ta1.Description
FROM tableA ta1
JOIN tableB ta2 on ta1.ID = ta2.ID
WHERE ta2.TableAId1=ta1.ID OR ta2.TableAId2=ta1.ID  OR ta2.TableAId3=ta1.ID  OR ta2.TableAId3=ta1.ID
or ta2.TableAId4=ta1.ID


The above solution is with foreign key(SQL Fiddle[^])
 
Share this answer
 
v8

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