MySQL Rounding functions - mysql

I am looking for a ROUND() type function that would allow me to round numbers to 1 decimal place but also to the nearest 0.5.
To illustrate:
19.425 => 19.5
19.124 => 19.0
Similarly:
12.654 => 12.5
12.845 => 13.0

As vissi said, to get the result you want you'll need 2 round statements. (To get to 1 decimal place)
SELECT ROUND(ROUND(19.425 * 2) / 2, 1) #19.5
SELECT ROUND(ROUND(19.124 * 2) / 2, 1) #19.0
SELECT ROUND(ROUND(12.654 * 2) / 2, 1) #12.5
SELECT ROUND(ROUND(12.845 * 2) / 2, 1) #13.0

You can multiply your number by two, round and then divide by two. Note, that the result may still be not very accurate (sth like 19.5000000000001).

Related

How to compute the standard deviation with a "number" column?

I have this table in MySQL :
value number_ads
1 3
2 1
3 1
3 1
4 1
I would like to compute the standard deviation of the column value, but taking into account that the value 1 for example should be counted 3 times.
The result should be :
AVG = 2.1429 STD = 1.124858267715973
I tried with this following request, but I don't have the good result:
SELECT
SUM(value * number_ads) / SUM(number_ads) AS avg,
SQRT((SUM(POW(value, 2)) - POW(2.1429, 2))/SUM(number_ads))
FROM `test`
Calculate the square root of variance. Variance is the difference between mean of (squares of values) and square of mean i.e, Sum(xx)/Count(n) - MeanMean.
SELECT
SUM(value * number_ads) / SUM(number_ads) AS avg,
SQRT((SUM(POW(value ,2) * number_ads)/SUM(number_ads)) - avg * avg)
FROM `test`
Source

MySQL selecting range between data

So I have my columns listed as 10, 20, 30, 40, 50 and would like mySQL to select and round up numbers between two selected ranges. So for queries BETWEEN 11 AND 32 I would like it to include the data between 10 and 40.
As is, simply using BETWEEN 11 AND 32 will only get me the values listed between 20-30. Rounding to the nearest 10 also does not alleviate this problem.
try
BETWEEN (FLOOR(lower/10)*10) AND (CEIL(upper/10)*10)
with lower and upper to be integers like 11 and 32, which will result in
(FLOOR(11/10)*10) => (FLOOR(1.1)*10) => 1*10 => 10
(CEIL(32/10)*10) => (CEIL(3.2)*10) => 4*10 => 40
x BETWEEN y AND z is the functional equivalent of (y <= x) AND (x <= z). if you want records "outside" of the specified range, then you have to widen your range, or modify the out-of-range values to be IN range.

how to round of decimal digit to nearest 50

How can it convert or round off the decimal digit to nearest 50. for e.g if i get 2.00 to 2.49 then it may change to 2.50 ,
2.50 to 2.99 then it may change to 3.00.
pls solve if anyone knows.
Something like this should produce your required rounding up:
SELECT CEILING(<input> * 2.0) / 2.0
Where <input> is the column or expression that's currently producing the values you want to round.
I don't know if this is an efficient way or not but I tried this :
IF OBJECT_ID('MyTable','U') IS NOT NULL
DROP TABLE MyTable
GO
CREATE TABLE MyTable (num DECIMAL(10,2))
GO
INSERT INTO MyTable
values (1.00),(1.01),(1.49),(1.50),(1.51),(1.99),(2.00),
(11.00),(11.01),(11.49),(11.50),(11.51),(11.99),(12.00)
SELECT [num],
CASE
WHEN ( [num] - [Nbr] ) BETWEEN 0.01 AND 0.49 THEN [Nbr] + 0.5
ELSE [Nbr]
END AS [Result]
FROM (SELECT [num],
ROUND(num, 0) AS [Nbr]
FROM MyTable) t
Note: Case when condition can be modified as per requirement.

MySQL - how to round

I have this in one of my queries:
FORMAT(ROUND(AVG(`rating` * 2)) / 2, 1) AS `average_rating`
If rating has a value of, say, 1.45 - then I get 1.5, which is what I want. When it rounds to whole numbers, I get 1.0, 2.0, etc. I don't want that. When I get whole numbers, I'd like to drop the decimal value.
Is there a way to do this in MySQL?
Try in this way :
(TRIM(TRAILING '.' FROM(CAST(TRIM(TRAILING '0' FROM
FORMAT(ROUND(AVG(`rating` * 2)
) / 2, 1))AS char)))) AS `average_rating`

Determine the range category of a specified number

So I have a column with different numbers and wish to categorize them by range within 30 minute intervals. So 5 would be 0-30, 697 would be 690-720, and 169 would be 150-180. I was first thinking of doing a case statement, but it doesn't look like Access 2003 supports it. Is there perhaps some sort of algorithm that could determine the range? Preferably, this would be done within the query.
Thank you.
Take the integer portion of (number / 30) using the Int function and multiply it by 30 to get your lower bound, then add 30 to that number to get your upper bound.
Examples
Int(5 / 30) = 0 * 30 = 0
Int(697 / 30) = 23 * 30 = 690
Use / (integer division) and * (multiplication).
5/30*30 = 0
697/30*30 = 690
169/30*30 = 150
...
Let x be your column with the values you want to catalogue, the in pseudo-SQL you have:
select ((x/30)*30) as minrange,
(((x/30)+1)*30) as maxrange
from yourtable
(you should take the integer part of the division).
Hope this helps.
This is fairly straight forward. You can just use the following.
(number \ 30) * 30
This will give you the lower index of your range. It does have one problem, which is that 30, 720, 180 etc, will be returned as themselves. This means your ranges either need to be 0-29, 690-719, etc, or have your caller take this into account.
This assumes you are using VBA where the '\' operator returns only the quotient. See more on VB operators here