May 1, 2014 at 8:43 am
Hi,
I trying to do AVG function in SQL and seems it does wrong calculation.
Data : -1, -1, 0
If we take average on the above data it gives result as 0.
The result should be as -0.6666
Is this a way sqlserver computes? any suggestions please
May 1, 2014 at 9:02 am
I don't know how your tables are set up, but if they are integers, SQL is correct. That is:
(-1 + -1 + 0)/3 = 0
using integer division
However, if they are floats:
with src(a) as (select -1. union all select -1. union all select 0)
select avg(a) from src
You get:
---------------------------------------
-0.666666
(1 row(s) affected)
May 1, 2014 at 9:54 am
thanks, got the difference. If it is float, it does.
Appreciate your time.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy