我有下表,我的目标是搜索与其他特定客户最相似的客户,了解他们对某些制造商的评价。此示例可在此startOf
中找到。
customer difference
D 4
B 7
C 11
示例:
我们希望找到哪些客户最适合客户' 谁拥有两家制造商的评级, manuf_A 和 manuf_B
期望的结果:
abs(A.manuf_A.rating (8) - B.manuf_A.rating(4)) = 4
abs(A.manuf_B.rating (3) - B.manuf_B.rating(Doesn't exist/0)) = 3
= 4 + 3
= 7
预期算法:
B差异:
abs(A.manuf_A.rating (8) - C.manuf_A.rating(Doesn't exist/0)) = 8
abs(A.manuf_B.rating (3) - C.manuf_B.rating(Doesn't exist/0)) = 3
= 8 + 3
= 11
C差异:
abs(A.manuf_A.rating (8) - D.manuf_A.rating(8)) = 0
abs(A.manuf_B.rating (3) - D.manuf_A.rating(7)) = 4
= 0 + 4
= 4
D差异:
{{1}}
关于如何在MySQL中完成此任务的任何建议都会得到很好的接受以及替代方法的任何建议。
答案 0 :(得分:2)
这是一种方法。为客户和制造商生成所有行," A"已评级。然后使用left join
查找任何给定客户评分的那些。其余的只是算术:
select c.customer,
sum(abs(am.rating - coalesce(cd.rating, 0))) as similarity
from (select cd.manufacturer, cd.rating
from centraldatabase cd
where cd.customer = 'A'
) am cross join
customers c left join
centraldatabase cd
on cd.manufacturer = am.manufacturer and cd.customer = c.customer
group by c.customer
order by similarity asc;
这是SQLFiddle。注意:我不知道在SQL Fiddle中创建自己的架构是否是个好主意。