Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 陷入排序算法_Sql Server_Algorithm_Ranking - Fatal编程技术网

Sql server 陷入排序算法

Sql server 陷入排序算法,sql-server,algorithm,ranking,Sql Server,Algorithm,Ranking,我陷入了一个我几天都在研究的算法中。是这样的: 我有很多帖子,人们可能喜欢也可能不喜欢。在从0到100的范围内,算法首先显示最受欢迎的帖子。但是当新的帖子出现时,他们还没有得分,所以他们进入了这个排名的末尾。我所做的:当一篇文章没有投票权时,我会给出一个默认分数(例如,75分) 当第一个用户喜欢这个新帖子时,它会得到总分(100分),但当用户不喜欢它时,它会进入列表的末尾(0分) 我该怎么做才能根据喜欢它的用户总数来获得喜欢的帖子的排名呢 如果我不够清楚,请告诉我 任何帮助都将不胜感激 到目前为

我陷入了一个我几天都在研究的算法中。是这样的:

我有很多帖子,人们可能喜欢也可能不喜欢。在从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

我对这个问题的解决办法是从估计值中减去一个标准误差。我会把这个变量视为所有回复中喜欢的比例。标准误差为:sqrt(plikes*(1-plikes)/(likes+notlikes))

在SQL中,这类似于:

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

减去一个标准误差有些随意,尽管它有统计基础。我发现这在实践中效果相当不错。

我不知道我是否正确理解了您的要求,但无论如何,我的答案是这样的。排名系统可能基于正面投票(喜欢)的平均值,这意味着排名=喜欢的数量/(喜欢的数量+不喜欢的数量)

在SQL中,您有如下内容:

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

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

我将使用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 .

谢谢你的帮助,但我已经这样解决了我的问题:

我用一个附加子句维护了原始查询

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
因此,如果一个新帖子进来,它将被分配默认值,直到第五个用户给它打分。如果内容真的很糟糕,它会排在列表的末尾,否则它会一直排在前面,直到其他用户对它进行评分


也许这不是一个完美的解决方案,但对我来说很有效

网络上有这样的东西。