I have 2 tables that I join using LEFT JOIN, i.e. I take Name from table A and match it to Name from table B. There are some records that do not match and return null values due to it being formatted differently.
Now what I want to do is wherever it isn’t matching, I want to extract the first word in Names from table A and search for it in names in Table B and return those values.
Could someone please help me achieve this? I am giving example below:
Table A:
Names |
---|
Allianz games |
Beta Test |
Car Company |
Table B:
Names | Type |
---|---|
Allianz games ltd | 1 |
Beta Test | 2 |
Car Company | 3 |
Output I am getting using LEFT JOIN:
A.Names | B.Names | Type |
---|---|---|
Allianz games | Null | Null |
Beta Test | Beta Test | 2 |
Car Company | Car Company | 3 |
Output I require:
A.Names | B.Names | Type |
---|---|---|
Allianz games | Allianz games ltd | 1 |
Beta Test | Beta Test | 2 |
Car Company | Car Company | 3 |
2
Answers
You could try two joins – first using an exact match, the second using a ‘LIKE’ match but only if there isn’t an exact match:
You’ll need to take care with this, however, because there could potentially be multiple matches in Table B.
So, you already have a
and you successfully find exact matches. You, however want to display results if the second table contains the first. You can get all records like this:
but this will provide all results and if you want a single result for each A, then you can group by and aggregate, like this:
Finally, if you want to include cases where A.Names contains B.Names, then you can change your join condition to