[英]How to rank by counting data in multiple rows in a column with certain conditions on it in MySQL
[英]MySQL how to rank objects by similarity of multiple property rows
大家好,新年快乐
情况:
我在MySQL db中有一些表:
分数:(唯一ID,唯一(objectID,metricID))
| ID | ObjectID | MetricID | Score |
|--------+----------+----------+----------|
|0 | 1 | 7 | 0 |
|1 | 5 | 3 | 13 |
|2 | 7 | 2 | 78 |
|3 | 7 | 3 | 22 |
|.....
|--------+----------+----------+----------|
对象:(唯一ID,唯一ObjectName)
| ID | ObjectName |
|--------+------------|
|0 | Ook |
|1 | Oop |
|2 | Oww |
|3 | Oat |
|.....
|--------+------------|
指标:(唯一ID,唯一MetricName)
| ID | MetricName |
|--------+------------|
|0 | Moo |
|1 | Mar |
|2 | Mee |
|3 | Meep |
|.....
|--------+------------|
对于给定的对象ID:
需求:
对于给定的ObjectID,我想根据以下条件返回排序列表:
问题陈述:
我不知道用于此的正确SQL语法,到目前为止我的实验都失败了。 我想尽可能多地在DB中完成这项工作,并且在代码或类似的讨厌的for循环中完成这项工作很少或根本没有。
其他非功能性
这是一种基于对象与对象1的相似性来对对象进行排序的方法:
select other.ObjectID
, avg(abs(target.Score - other.Score)) as Delta
from Scores target
join Scores other
on other.MetricID = target.MetricID
and other.ObjectID <> target.ObjectID
where target.ObjectID = 1
group by
other.ObjectID
order by
Delta
相似性定义为常见指标的平均差异。 未列出不与对象1共享至少一个度量的对象。 如果这个答案做出错误的假设,请随时澄清你的问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.