AVG*COUNT return a float value - mysql

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)

Related

Setting A Column to the 100th dec in SQL Query

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)

mysql date direct subtraction

I have a two date columns, dateA and dateB. If I subtract dateA from dateB (dateB - dateA), I get wrong results but not using DATEDIFF(dateB,dateA)function. I get wrong result from direct subtraction when used in a table with data but not with below query.
SELECT DATE('2013-01-31') - DATE('2013-01-27')
Why?
EDIT:
I found that in MySQL if the two dates are within a month then direct subtraction gives correct result but if the dates span month, year there might be a problem.
Am I right?
Presumably, you columns are not stored as dates, but as strings. If they were stored as dates, then subtraction would work as expected.
When you subtract two strings, such as:
SELECT '2013-01-31' - '2013-01-27'
Then MySQL converts them to numbers, based on the leading digits. In this case, both start with the numbers 2013, so both are converted to 2013 (if there were no digits at the beginning then the value would be 0). These numbers are then subtracted.
Are your column's defined as date, or as Varchars? Because Date as a String means nothing. When you do a DATE('2013-01-31'), it creates a date object and hence the subtraction works. So, if your column is defined as Varchar, do DATE(dateB) - DATE(dateA).

MySQL sum with negative decimals

I have a qty column in my table, and i'm trying to sum the values. The field type is varchar(20). The sum of qty below should add up to exactly 0. It's a mix of negative and positive decimals.
exhibit A (screenshot)
When i perform the sum below, i'm getting a tonne of decimals instead of 0. I'm assuming this is a datatype issue. What's the best way to work around this?
exhibit B (screenshot)
You should not be storing numeric data as a string but if you do, then you will need to cast() it to apply a SUM() aggregate to it:
SELECT SUM(CAST(yourcolumn AS DECIMAL(10, 2)))
FROM yourtable
So your query will be:
select sum(cast(qty as DECIMAL(10, 2)))
from inventory i
where i.refDocNum = 485
and i.refApp = 'WO'
and i.type in (20, 21)

MySQL ROUND() calculates incorrectly

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.

What does the first argument to MySQL's Decimal function do?

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.