I have these tables:
Table 1:
Code1 Code2 Code3 Code4 ISCode5
-----------------------------------
xx NULL TEST1 TEST1 1
yy zzz TEST2 TEST2 1
NULL ss TEST3 TEST3 1
NULL aaa TEST4 TEST4 0
Table 2:
ID ColTest1 ColTest2 Code1 Code2
-----------------------------------------
1 2 3 xx NULL
1 3 4 yy zzz
2 5 6 NULL ss
2 5 6 NULL aaa
Expected output:
ID ColTest1 ColTest2 Code1 Code2
------------------------------------------
1 2 3 xx NULL
1 3 4 yy zzz
2 5 6 NULL ss
I need to join table1 and table 2 where IsCode5 = 1. But there is no primary key and foreign key for both tables. The only common columns for both tables is Code1 and Code2.
I have tried this:
SELECT
T2.ID, T2.ColTest1, T2.ColTest2, T2.Code1, T2.Code2
FROM
[dbo].[table2] T2
INNER JOIN
[dbo].[table1] T1 ON T1.Code1 = T2.Code1
AND T1.Code1 = T2.Code2
WHERE
T1.ISCode5 = 1
But it only returns:
ID ColTest1 ColTest2 Code1 Code2
-----------------------------------------
1 3 4 yy zzz
Do you have any idea to return the expected result? Thanks.