坚持排名算法

时间:2014-12-18 03:12:11

标签: sql-server algorithm ranking

我遇到了一个我几天都在工作的算法。它是这样的:

我有很多帖子,人们可能喜欢或不喜欢他们。在0到100的范围内,算法首先显示最喜欢的帖子。但是当新帖到达时,他们还没有得分,所以他们到了这个排名的末尾。我做了什么:当一个帖子没有任何投票时,我把默认分数(例如,75)。

当第一个用户喜欢这个新帖子时,它会得到总分(100),但是当用户不喜欢它时,它会进入列表的末尾(得分0)。

根据喜欢它的用户总数,我可以做些什么才能为喜欢的帖子实现此排名?

如果我不够清楚,请告诉我

任何帮助将不胜感激。

到目前为止我做了什么:

select id,(
(select cast(count(1) as float) from posts p1 where p1.id = p.id and liked = 1) /  
(select cast(count(1) as float) from posts p2 where p2.id = p.id)
)*100 AS value
from posts p  
group by id

4 个答案:

答案 0 :(得分:3)

我对此问题的解决方案是从估计值中减去标准误差。我会将所讨论的变量视为对帖子的所有回复中喜欢的比例。标准错误是:sqrt(plikes *(1-plikes)/(喜欢+ notlikes))。

在SQL中,这将是:

select id,
       (avg(liked*1.0) - sqrt(avg(like * 1.0) * avg(1.0 - like) / count(*))) as like_lowerbound
group by id;

减去一个标准误差在某种程度上是任意的,尽管它有一个统计基础。我发现这在实践中非常有效。

答案 1 :(得分:1)

我不知道我是否理解你想要什么,但无论如何,这是我的回答。排名系统可以基于肯定投票(喜欢)的平均值,这意味着,排名= number_of_likes /(number_of_likes + number_of_dislikes)。

在SQL中,你有这样的东西:

SELECT id, (likes/(likes + dislikes)) as rank FROM posts order by rank desc;

如果您需要将结果设置在[0,100]之间,而不是[0,1],则可以乘以100。

答案 2 :(得分:1)

我将使用union all编写两个查询,而不是进入如此复杂的情况。

---First query to select fresh post 
--also include one column which can be use in order by clause
-- Or you can make your own indication
Select col1,col2 .......,1 Indicator from post where blah blah
Union all
--Second query to select most populare
Select col1,col2 .......,2 Indicator from post where blah blah

然后在前端,您可以轻松识别并进行过滤。

Also it is easy to maintain and quite fast .

答案 3 :(得分:0)

感谢您的帮助,但我以这种方式解决了我的问题:

我使用附加条款

维护原始查询
select id,(
(select cast(count(1) as float) from posts p1 where p1.id = p.id and liked = 1) /  
(select cast(count(1) as float) from posts p2 where p2.id = p.id)
)*100 AS value,
(select count(1) from posts p3 where p3.id = p.id) as qty
from posts p  
where qty > 5
group by id

因此,如果有新帖子进入,则会在第五个用户对其进行评分之前分配默认值。如果内容真的很糟糕,它会进入列表的末尾,否则它将保持最佳状态,直到其他用户对其进行评分为止。

可能不是完美的解决方案,但为我工作

相关问题