I am having the problem with MySQL ROUND() Function.
Here it is:
When I do this:
SELECT ROUND(7/2) as avg FROM bla blah
avg is 4
When I do this:
SELECT ROUND(SUM(marks)/COUNT(marks)) as avg
avg is 3
Note: SUM(marks) on its own gives 7 and COUNT(marks) gives 2 which as far as I understand
ROUND(SUM(marks)/COUNT(marks)) should be equal to ROUND(7/2)
What is the problem?
Apparently MySQL interprets 7/2 as 7.0/2.0 and does the calculation using floating point numbers instead of integers, giving the result 3.5 rather than 3.
When you use sum the data type of the result is the same as the field, so you will be doing the calculation using integers, i.e. 7/2, giving the result 3.
Cast the values to double before doing the calculation:
SELECT ROUND(cast(SUM(marks) as double)/cast(COUNT(marks) as double)) as avg
You might use CEILING (same as ROUND except it always rounds to the bigger nearest integer)
SELECT CEILING(SUM(marks)/COUNT(marks)) as avg
should yield the result you want for the requirements specified in the question.
MySql mathematical operations create float results.
When using floats 7/2 could be represented as 3.4999999999999. Rounding that yields 3.
Related
I am writing a query that is used by report generating software.
Part of this is querying for the hours needed to complete a project. We record this a 2 decimal float so that we can estimate to the quarter hour.
However, if we are using it in our report and the hour we recorded is something like 8.00, I want to query it and format it so that 8.00 is just 8. However any hours with something past the decimal, like 8.25, should remain as 8.25. How can I make this work?
hours Queried Result
====== -> My Query -> ==============
8.00 8
8.25 8.25
I am using MySQL 5.6
You can use the REPLACE() function to remove .00:
REPLACE(hours, '.00', '') AS hours
You can convert it to a string and check the rightmost 2 characters and trim those if they are '00'.
SELECT TRIM(TRAILING '.00' FROM CAST(column_name AS VARCHAR));
SELECT REPLACE(Round(8.00), '.00', ' ');
I will give more example so you can clear your Logic:
MySQL ROUND() rounds a number specified as an argument up to a number specified as another argument.
Syntax:
ROUND(N,[D]);
Where 'N' is rounded up to D decimal places.
and 'D' is indicating up to how many decimal places N will be rounded.
Example 1:-
SELECT ROUND(4.43);
Output :-
4
The above MySQL statement will round the given number 4.43. No decimal places have been defined, so the default decimal value is 0.
Example 2:-
SELECT ROUND(-4.53);
Output:-
-5
The above MySQL statement will round the given number -4.53. No decimal places have been defined, so the default decimal value is 0.
Why does this SELECT statment return a float value? How can I cast it into an integer?
select area.name, coalesce(((avg(recording.length)*count(recording.length))/1000/60),0)
If the result of avg(recording.length)*count(recording.length) isn't a multiple of 60000, you'll get a fraction when you do that division. You can use ROUND() to round this to the nearest integer.
Also, since the average is the sum divided by the count, multiplying by the count just returns the sum. You can just use that directly.
SELECT area.name, coalesce(ROUND(SUM(recording.length)/1000/60)), 0)
I have a column with bytes, and another with milliseconds. And I must calculate average bitrate in bits per second.
I'm doing this:
SELECT AVG(Bytes*8)/AVG(Milliseconds/1000)
FROM Tracks
Apparently it is wrong. I'm using an app with exercises
I have this result
254492.61
And should be
254400.25
I think you only want one average calculation
SELECT AVG((Bytes*8.0)/(Milliseconds/1000.0))
FROM Tracks
and you may want to increase precision to decimals which is why 8.0 and 1000.0 are used above. Remove if unwanted.
I would be inclined to write this as:
SELECT SUM(Bytes*8) / SUM(Milliseconds/1000)
FROM Tracks
This is equivalent to your query, though -- assuming that the values are never NULL.
Perhaps they mean the average of averages:
SELECT AVG(Bytes * 8 / (Milliseconds / 1000))
FROM Tracks;
I would not describe this as the average bits per second, however.
I have a variance report query here I need the 'Variance' to not have 10 decimal points in the Variance Column. What is the most convenient way to round Variance results to the 100th?
WITH A AS
(
select
A.FACTORY,
A.JOB_NUMBER,
A.PROCESS_STAGE,
A.PART_CODE,
B.PART_DESC_1,
A.INPUT_QTY_STD,
A.QUANTITY_INPUT,
A.QUANTITY_OUTSTANDING,
A.INPUT_QTY_ACTUAL,
(A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)) as variance,
A.ACTUAL_CLOSE_DATE
from
(select * from [man_prod].[dbo].[JOB_STAGE_LINES]
where JOB_NUMBER in (select JOB_NUMBER from JOB_OUTPUTS where
BF_QTY_ACTUAL<>0
and ABS(DATEDIFF(HOUR,ACTUAL_CLOSE_DATE,GETDATE())) < 12 and STATUS_FLAG='C'
)) A
join fin_prod.dbo.PRODUCT_MASTER B
ON A.PART_CODE=B.PART_CODE
WHERE
A.INPUT_QTY_STD<>0 and
A.QUANTITY_OUTSTANDING <>0
)
SELECT * FROM A WHERE A.variance >10.000000 OR A.variance <-10
order by PROCESS_STAGE asc ,PART_CODE asc, variance desc ;
The Variance column comes out at 00.0000000000 i need it to display 00.000 or 00.000000
Help is greatly appreciated
Use the MySQL ROUND() function, the second argument is the number of decimal places if it is positive.
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
In this example if the value is 0.0000000000 it would be rounded to 3 decimal places, or 0.000.
You can use the TRUNCATE option:
TRUNCATE((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
or use the ROUND if you are looking for rounding(as suggested by doublesharp)
ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,
Using Convert to convert it to a decimal of the desired length is what i prefer when i am not actually rounding the value, just formatting.
CONVERT(DECIMAL(10,3),10000)
Using MySQL 5.0.27
This query:
SELECT CAST('543.21' AS DECIMAL(100,2))
returns 543.21
So does this one:
SELECT CAST('543.21' AS DECIMAL(2,2))
In fact, I am having trouble figuring out what effect the parameter has. I am using it to aggregate numeric values in a varchar column (for legacy reasons!!) and round off to 2 decimal places.
Should I just pick a high number?
It describes how many total digits a field (or variable) will be able to store.
DECIMAL(100,2) - 100 total digits, 98 before, 2 after a decimal separator
DECIMAL(2,2) 2 total digits, 0 before, 2 after a decimal separator
Explained here:
http://dev.mysql.com/doc/refman/5.1/en/numeric-types.html
[added]
For rounding just use ROUND() function.