Mysql
 sql >> Database >  >> RDS >> Mysql

Valutazione MySQL con peso

Un primo passo è calcolare avg_num_votes e avg_rating :

SELECT
  SUM(totalVotes)/COUNT(*) AS avg_num_votes,
  SUM(avgVote)/COUNT(*) AS avg_rating
FROM voting;

Se riesci a convivere con un piccolo errore, potrebbe essere abbastanza buono calcolarlo una volta ogni tanto.

Ora, usando la tua formula e i valori sopra, puoi eseguire la query di pesatura. Come piccola ottimizzazione precalcolo avg_num_votes * avg_rating e chiamalo avg_summand

SELECT
  voting.*, -- or whatever fields you need
  ($avg_summand+totalVotes*avgVote)/($avg_num_votes+totalVotes) AS bayesian
FROM voting
ORDER BY  bayesian DESC
LIMIT 1;

Modifica

Puoi eseguirlo come un join:

SELECT
  voting.*, -- or whatever fields you need
  (avg_num_votes*avg_rating+totalVotes*avgVote)/(avg_num_votes+totalVotes) AS bayesian
FROM voting,
(
  SELECT
    SUM(totalVotes)/COUNT(*) AS avg_num_votes,
    SUM(avgVote)/COUNT(*) AS avg_rating
  FROM voting AS iv
) AS avg
ORDER BY  bayesian DESC
LIMIT 1;

Ma questo calcolerà la somma e la media su ogni singola query:chiamala una bomba delle prestazioni.