I have a requirement to create SQL Server user defined function/SP (either normal or table valued function) which has the below requirements:
- The data across 4 tables (Table_A, Table_B, Table_C, Table_D) should be matched based on fix attributes (Name in our below example)
- If the data matches in all the 4 tables it gets the highest score & uniqueID is created. For example Match Type = ABCD
- If the data matches in other combinations of 3 tables than it gets some score and different UniqueID. For example Match Type = ABC, ABD, BCD, CDA
- If the data matches in other combinations of 2 tables than it gets some score and different UniqueID. For example Match Type = AB, AC, AD, BC, BD, CD
- Records that doesn't match will get 0 score with separate UniqueID will be stored in the same table.
Table_A
AID | Name | ZipCode
Table_B
BID | Name | ZipCode
Table_C
CID | Name | ZipCode
Table_D
DID | Name | ZipCode
It matches on Name and ZipCode attributes
Final or match and merge table:
UID | AID | BID | CID | DID | Match_Score
Please suggest how can we create a function/stored procedure for the above requirements. If we can make it robust and expandable would be better i.e. If one more tables get added the logic should work with minimal code changes.
Really appreciate your help in this case.
I can think of the below approach but not sure if that can be coded -
ABCD (Output of the table where all the record matches)
UNION ALL
ABC (This will run only on the records that are not par of the ABCD result)
UNION ALL
ACD (This will run only on those records which are not a part of the above 2 results)
UNION ALL
and on and on