开发者

T-SQL Rounding to 0.0, 0.5, or 1.0

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 -> 开发者_JAVA技巧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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜