Here is the Scenario :
I have 4 tables table1(COL1,COL2,cCOL3,COL4),table2(COL1,COL2,COL3 ,COL4,COL5),table3(COL1,COL2,COL3,COL4,COL5),table 4(COL1,COL2,COL3,COL4,COL5,COL6,COL7)
Table1
COL1,COL2 is the Primary key in Table 1
Table2
COL3,COL4 are the primary key in Table 2 and COL1,COL2 is the foreign key which references table 1
Table3
COL5 is the primary key of Table3 and COL1,COL2 are the foreign key references Table1
and COL3,COL4 are the foreign key references Table 2
Table4
COL6 is the Primary key in Table4 and COL1,COL2 are the foreign key references Table1
and COL3,COL4 are the foreign key references Table 2 and COL5 is the FK ref Table3
QUERY : i need to fetch the data from Table4 (COL7) where COL1,COL2 of table 4 must be equal to COL1,COL2 of table 1
COL3,COL4 of table 4 must be equal to COL3,COL4 of table 2
COL5 of table 4 must be equal to COL5 of table 3
SELECT T4.COL7
FROM Database.{SCHEMANAME}.{TABLE4}
AS T4 WHERE
(
SELECT T3.COL5
FROM Database.{SCHEMANAME}.{TABLE3}
AS T3 WHERE T3.COL5 = T4.COL5
)
AND
(
SELECT T2.COL3,T2.COL4
FROM Database.{SCHEMANAME}.{TABL2}
AS T2
WHERE T2.COL3 = T4.COL3
AND T2.COL4 = T4.COL4
)
AND
(
SELECT T1.COL1,T1.COL2
FROM Database.{SCHEMANAME}.{TABLE1}
AS T1
WHERE T1.COL1 = T4.COL1
AND T1.COL2 = T4.COL2
);
Could u guys please help me to rephrase the Query ?