I have to match a table of Agents to a table Clients based on a varying number of matching columns, some match on all columns, others only match on one, or two or three; and not the same columns. Examples:
Clients table has columns
ClientID, System, Country, Office
Example data:
01 ABC US CT
02 ABC US RI
03 ABC US MA
04 DEF US AZ
04 GHI CA PQ
05 ABC ZZ ZZ
Agents table has columns
AgentID, System, Country, Office
Example data:
A1 ABC US CT
A2 ABC RI
A3 ABC US MA
B1 DEF US
B2 GHI
Problem: All Agents have a System value, but some Agents have no values for Country or Office, some have only System, some have System and Country, some have System and Office, and the rest have all 3: System, Country and Office. If a Client only matches 1 or 2 fields to an Agent, they're treated like "wildcards" and are considered a match for the appropriate Agent.
How can I create a View that matches every Client and Agent? And shows any Clients that have no corresponding Agent, such as Client 05 above?
So far all I've been able to come up with is to create some kind of table and append the records in 3 or 4 steps, but that is so messy and clumsy, and adds another table to the mix.
I tried a COALESCE statement, and joining with a LIKE statement and surrounding fields with %, but nothing I've tried gives valid results.
Would this call for a Temp table or a CTE? Or something? I'm stuck!
Any ideas?