这是经典的问题,如何对upvote / downvote,正/负,喜欢/不喜欢等进行排名。有几种可能的解决方案,但在特定条件下可能会给出错误的结果。
我强烈建议您阅读和使用像 How Not To Sort By Average Rating
问题:
您需要某种“分数”来进行排序。
错误的解决方案#1:得分=(正面评分)-(负面评分)
错误的解决方案#2:得分=平均评分=(正面评分)/(总评分)
正确的解决方案:分数= Bernoulli参数的Wilson分数置信区间的下限
示例代码(您可以轻松地根据需要对其进行调整):
SELECT id, ((positive + 1.9208) / (positive + negative) -
1.96 * SQRT((positive * negative) / (positive + negative) + 0.9604) /
(positive + negative)) / (1 + 3.8416 / (positive + negative))
AS ci_lower_bound
FROM your_tab
WHERE positive + negative > 0
ORDER BY ci_lower_bound DESC;
Run Code Online (Sandbox Code Playgroud)