I have an interesting issue where I need to round the result of an AVG() function in a specific manner for use in the ORDER BY clause.
These are for use in recipe ratings.
Examples of the rounding formula:
1.2 -> 1
1.4 -> 1
1.5 -> 1.5
1.6 -> 2
1.9 - >2
I am querying for a list of recipes, and they needed to be ordered so that a recipe with one 5-star rating is not ordered a above a recipe with 100 ratings that average 4.9. The second part of the order by clause is the count of ratings.
If this requires a user defined function, I'm not quite sure how to go about doing it.
ORDER BY
CASE WHEN (Num % 1) = .5
THEN Num
ELSE ROUND(Num,0)
END
If I understood correctly, you have stars and half-stars? I'd suggest ORDER BY ROUND(value*2, 0)/2
, this way it rounds to closest 0.5
(half-star) step.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.