3
I have this query on Mysql:
SELECT
dealers.id,
COALESCE (AVG(rating), 0) AS media,
COALESCE (COUNT(dealer_ratings.id), 0) AS qtd_avaliacoes
FROM
`dealers`
LEFT JOIN `dealer_ratings` ON `dealer_ratings`.`id_concessionaria` = `dealers`.`id`
AND `id_status` IN (1, 2)
WHERE
`id_cidade` = 5141
AND `confirmada` = 1
GROUP BY
`dealers`.`id`
ORDER BY
`media` DESC,
`qtd_avaliacoes` DESC
Brings me that result:
ID Média Quantidade
11393 5.0000 1
12291 4.5000 12
11918 0.0000 0
14078 0.0000 0
I can’t order by MEDIA and QTD_AVALIACOES and not the other way around, QTD_AVALICOES and MEDIA. If I do it in one of these ways, it might work in some cases and not in others.
So I need to sort by average weight. But I don’t know what calculation to do.
In the case of the example above, the result with an average of 4.5 and 12 of quantity would be to be in first, because proportionally it is better.
How do I do that ?
Want to sort by the result MEDIA*QTD_AVALIACOES on each right line?
– Miguel
I want to make a weighted average, if I’m not mistaken. I can’t explain, I’m Newbie.
– Diego Souza
Don’t say that. Okay but what do I calculate? MEDIA*QTD_AVALIACOES on each line? what mathematical operation do you want to do to then sort?
– Miguel
I want to calculate the average weight. I believe that’s what you said.
– Diego Souza
But what weight? What is the weight?... I will put a solution to the problem, if not say to withdraw the answer
– Miguel
Why did you remove the reply @Miguel. It had helped me.
– Diego Souza
Ha I’m sorry, I thought I was wrong and that not what I wanted... I’ll put
– Miguel
It’s just that I’m doing tests, so I didn’t give feedback.
– Diego Souza
Okay, that’s it. I’m sorry
– Miguel