Answer the question
In order to leave comments, you need to log in
How to properly design tables for calculating the rating of a post based on the lower bound of the Wilson confidence interval?
There is a good translation of an article on Habré , according to which it is recommended to sort posts by the lower bound of the Wilson confidence interval. It provides the formula and implementation of the SQL query:
SELECT
widget_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 widgets WHERE positive + negative > 0
ORDER BY ci_lower_bound DESC;
mood
, which can store only two values negative
and positive
, and a column - the rating
rating of this post, which the user leaves for the post.+----+---------+--------+----------+-------------+------------+
| id | post_id | rating | mood | message | created_at |
+----+---------+--------+----------+-------------+------------+
| 1 | 1 | 3 | positive | Some string | 26/01/2021 |
+----+---------+--------+----------+-------------+------------+
| 2 | 2 | 2 | negative | Some string | 26/01/2021 |
+----+---------+--------+----------+-------------+------------+
| 3 | 1 | 1 | negative | Some string | 26/01/2021 |
+----+---------+--------+----------+-------------+------------+
$post->reviews->positive()->count()
eitherSELECT COUNT(*) FROM reviews WHERE mood = 'positive'
, but I have to somehow calculate this, before executing the SQL query from the article and store these two values \u200b\u200b(number of positive and negative ratings) somewhere.
Answer the question
In order to leave comments, you need to log in
I think you can make a view with something similar (I don’t know what subd you have)
Select
post_id,
sum(case when mood == 'positive' then 1 else 0 end) as count_positive,
sum(case when mood == 'negative' then 1 else 0 end) as count_ negative
FROM Reviews
GROUP BY post_id
SELECT
widget_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 widgets WHERE positive + negative > 0
ORDER BY ci_lower_bound DESC;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question