I am trying to join the below tables:
Table A:
Type1 Type2 ID Object Location
Sample Dummy 1 X111 111222
Sample Dummy 2 X333 333444
Sample Dummy 2 X333 555666
Sample Red 1 X222 666777
Test Red 1 X222 666777
Test Red 1 X222 666777
Table B:
Source Type2 ID Object Logged
SR1 Dummy 1 X111 17
SR1 Dummy 2 X333 1
SR1 Red 1 X222 12
SR2 Dummy 1 X111 9
SR2 Dummy 2 X333 9
SR2 Red 1 X222 20
My SQL code:
SELECT B.Source, B.Type2, B.ID, B.Object, B.Logged, A.Location
FROM A RIGHT JOIN B ON
A.Type1='Sample' AND
A.Type2=B.Type2 AND
A.ID=B.ID AND
A.Object=B.Object
WHERE B.Source='SR2'
I am expecting the following results:
Source Type2 ID Object Logged Location
SR1 Dummy 1 X111 17 111222
SR1 Dummy 2 X333 1 333444
SR1 Dummy 2 X333 1 555666
SR1 Red 1 X222 12 666777
But I'm getting this instead:
Source Type2 ID Object Logged Location
SR1 Dummy 1 X111 17 111222
SR1 Dummy 2 X333 1 -
SR1 Dummy 2 X333 1 -
SR1 Red 1 X222 12 666777
What's wrong with my code? Please help.
LEFT JOIN
is much easier to follow. The issue is the filtering on b.Type1 = 'Sample'
. From what I can tell, that is not needed:
SELECT B.Source, B.Type2, B.ID, B.Object, B.Logged, A.Location
FROM B LEFT JOIN
A
ON A.Type2 = B.Type2 AND
A.ID = B.ID AND
A.Object = B.Object
WHERE B.Source = 'SR2';