I have two tables (main_table, codes_table). I need to join the two tables on column code. Example data below:
main_table
id | customer | code |
---|---|---|
1 | Fred | Acme Residential |
2 | Sue | Acme Business |
3 | Bud | Acme & Old State |
codes_table
id | code | group |
---|---|---|
1 | Acme | X19CD |
2 | Acme Business | G933C |
My data is in Google Big Query and I am hoping to use native SQL. I am trying to come up with SQL that would allow me to join main_table to codes_table such that I would get the following output:
Results
cust_id | customer | code | group |
---|---|---|---|
1 | Fred | Acme Residential | X19CD |
2 | Sue | Acme Business | G933C |
3 | Bud | Acme & Old State | X19CD |
Appreciate any thoughts on how one can accomplish this with SQL.
Essentially the code in the customer table could be various permutations of "Acme ". The issue I have had that trying the various ways I have to join to two tables ends up getting both code_table rows as they both start with "Acme". What I am trying to do is join the two where the main_table.code matches the most characters from the code_table.code.