我有一个特定的场景,在这种情况下,我必须根据某些给定的排列和组合规则分配适用于某些汽车导联的折扣。
要详细说明,我有以下两个表:
Below table contains some raw data of all leads of vehicle
并且:
Below Table contains the Rules (Permutation and combination) for all discounts
我想要的是根据Lead中规则匹配的最大变量数,从Rules中获得最佳规则。
如何为此编写SQL?
答案 0 :(得分:0)
像这样吗?
SELECT ld.Make, ld.FuelType, MAX(p.Discount)
FROM LeadData ld
LEFT JOIN Permutation p on (ld.City = p.City OR p.City IS NULL) AND (p.FuelType = ld.FuelType OR p.FuelType IS NULL) AND (ld.Make = p.Make)
GROUP BY ld.Make, ld.FuelType