Cross apply with closest match

断了今生、忘了曾经 提交于 2019-12-23 04:52:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!