Member since
11-03-2018
1
Post
0
Kudos Received
0
Solutions
11-03-2018
04:27 AM
Hi Nitin I was working on similar problem of dynamic rounding and found a workaround. In my case I do average price statistics and I want to round average prices to 3 significant digits, in order to group similar observations. Now every price has different number of digits, so e.g. I must round 1.495 to 1.50, but also 1099 should be rounded to 1100. Following expression did the trick for me: SELECT ROUND(`price` / POWER(10.0, (FLOOR(LOG10(`price`)) - 2)), 0) * POWER(10.0, (FLOOR(LOG10(`price`)) - 2)) AS `price_rounded` The -2 constant is related to my specific problem, i.e. 3 significant digits, so that could be adjusted or omitted in other use case. Conceptually what I do is that I reflect number of digits to round on by transforming the first argument, then I can always round to zero digits no matter what, and finally transform data back. May work better than CASE WHEN in situation when you don't know dimensions of your data upfront. BR, Mirek
... View more