![](/img/trans.png)
[英]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.