T-SQL 舍入为 0.0、0.5 或 1.0
我有一个有趣的问题,我需要以特定方式舍入 AVG() 函数的结果以在 ORDER BY 子句中使用。
这些用于食谱评级。
舍入公式的示例:
1.2 -> 1
1.4 -> 1
1.5 -> 1.5
1.6 -> 2
1.9 - >2
我正在查询食谱列表,并且需要对它们进行排序,以便具有 1 个 5 星级评级的食谱不会高于具有 100 个评级的平均 4.9 的食谱。 order by 子句的第二部分是评级计数。
如果这需要用户定义的函数,我不太确定如何去做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没理解错的话,你有星星和半星吗?
我建议使用
ORDER BY ROUND(value*2, 0)/2
,这样它会四舍五入到最接近的0.5
(半星)步长。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 closest0.5
(half-star) step.