SqlServer AVG Issue

  • 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

  • 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)

  • 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