April 18, 2009 at 10:10 pm
Hi there,
I'm having a hard time rounding avg results upwards (underlying dataype is int).
For example if the sum of 3 rows = 8, then avg should be 3 since 8/3 = 2,67. But I get 2.
I would like it to round up if the fraction > 0,5.
The code I use is like this:
ROUND(AVG(Atraso), 0) AS avgAtraso
I also tried: ROUND(CAST(AVG(Atraso)AS FLOAT), 0) AS avgAtraso, but to no avail.
I would like to know how this can be done without having to convert the underlying INT to FLOAT.
I appreciate any help
Regards,
Jaime
April 20, 2009 at 12:07 am
jaimepremy (4/18/2009)
Hi there,I'm having a hard time rounding avg results upwards (underlying dataype is int).
For example if the sum of 3 rows = 8, then avg should be 3 since 8/3 = 2,67. But I get 2.
I would like it to round up if the fraction > 0,5.
The code I use is like this:
ROUND(AVG(Atraso), 0) AS avgAtraso
I also tried: ROUND(CAST(AVG(Atraso)AS FLOAT), 0) AS avgAtraso, but to no avail.
I would like to know how this can be done without having to convert the underlying INT to FLOAT.
I appreciate any help
Regards,
Jaime
Hi jaime!
your code should be like this:
declare @AVG table
(
AVGSUM int
)
insert into @AVG
select 1 union all
select 2 union all
select 5
select round(avg(cast(avgsum as float)),0) from @AVG
BR
Hayzer
April 20, 2009 at 12:17 am
April 20, 2009 at 12:57 am
Hi,
can you try with the below code?
CEILING(AVG(Atraso))
April 20, 2009 at 6:28 am
Try this
ROUND(AVG(CAST(Atraso AS FLOAT)), 0) AS avgAtraso
Kemp
April 20, 2009 at 9:15 am
HI Kemp, thank u, works like a charm.
Chandra: CEILING(AVG(Atraso)) won't cut it: it rounds down to 2 from 2,67
As for the responses from Hayzer and Arun, I think u guys misunderstood my question.
But many thanks to all of u for your attention
Regards,
Jaime
April 20, 2009 at 5:47 pm
Here is some code for review. The three select statements are there to show the progression. Obviously, the final one combines everything.
declare @TBL table
(amt int)
insert into @TBL
select 1 union all
select 3 union all
select 4
select avg(amt * 1.00) as 'AVERAGE' from @TBL
select round(avg(amt * 1.00),2) as 'AVERAGE' from @TBL
select cast(round(avg(amt * 1.00),2) as numeric(7,2)) as 'AVERAGE' from @TBL
April 20, 2009 at 6:07 pm
jaimepremy (4/18/2009)
Hi there,I'm having a hard time rounding avg results upwards (underlying dataype is int).
For example if the sum of 3 rows = 8, then avg should be 3 since 8/3 = 2,67. But I get 2.
I would like to know how this can be done without having to convert the underlying INT to FLOAT.
The answer is no. You will need to CAST to a float (or other datatype that has decimal places).
The reason is that when you divide two integers, SQL will perform "integer division" and truncate the decimal place. This is the same for all programming & scripting languages.
April 20, 2009 at 8:48 pm
Mike, Goldie
Thank u for your answers and attention, but like I said earlier Kemp provided the solution:
ROUND(AVG(CAST(Atraso AS FLOAT)), 0) AS avgAtraso
regards,
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply