问题
I have a requirement to do matching of few attributes one by one. I'm looking to avoid multiple select statements. Below is the example.
Table1
Col1|Price|Brand|size|Color
-----------------------
A|10$|BRAND1|10|Red
B|25$|BRAND1|20|Red
C|30$|BRAND1|15|Red
D|40$|BRAND2|25|Blue
E|40$|BRAND2|30|Blue
F|35$|BRAND3|31|Blue
Table2
Col1|Col2|Col3
--------------
B|XYZ|PQR
F|ZZZ|YYY
Table3
Col1|COL2|COL3|LIKECOL1|ClosestPrice|brand|size|Color
------------------------------------------------
B|XYZ|PQR|C|25|BRAND1|15|Red
F|ZZZ|YYY|E|40|NULL|NULL|Blue
In table3, I need to insert data from table2 by checking below conditions.
Find a match for record in table2, if Brand and Color match and size and Price have the closest match. If no match found, then try just Color(exact match), Size and price closest match If still, no match found, try color exact match and price closest match.
In the above example, for the first record 'B' in table2, found a match as C and so inserted into table3 and second record F, record 'E' is matching but only Color and exact match.
Based on some previous posts, I can use cross apply and get the conditions of exact match included, but can anyone help me to include closest match logic too within this?
来源:https://stackoverflow.com/questions/53030624/cross-apply-with-closest-match