I have 2 simple tables that I would like to perform an INNER JOIN with, but the problem is that I’m getting duplicated (for the columns str1 and str2) results:
CREATE TABLE #A (Id INT, str1 nvarchar(50), str2 nvarchar(50))
insert into #A values (1, 'a', 'b')
insert into #A values (2, 'a', 'b')
CREATE TABLE #B (Id INT, str1 nvarchar(50), str2 nvarchar(50))
insert into #B values (7, 'a', 'b')
insert into #B values (8, 'a', 'b')
select * from #A a
INNER JOIN #B b ON a.str1 = b.str1 AND a.str2 = b.str2
It gave me 4 records when I really wanted 2.
What I got:
id | str1 | str2| id | str1 | str2
1 | a | b | 7 | a | b
2 | a | b | 7 | a | b
1 | a | b | 8 | a | b
2 | a | b | 8 | a | b
What I really wanted:
1 a | b | 7 | a | b
2 a | b | 8 | a | b
Can anyone help? I know this is achievable using a cursor and loop, but I’d like to avoid it and only use some type of JOIN if possible.
If you have more rows in one or the other tables for the same
(str1, str2)combination, you can choose which ones will be returned by changingINNERjoin to eitherLEFT,RIGHTorFULLjoin.