Show Fractional Average Age

  • Hello all,

    I have a field called age in my table and the data type is int. How would I calculate the fractional average? For example, I have the below account holders and their age. I want to show the average age

    as 45.75

    Data Age

    Account 1 46

    Account 2 36

    Account 3 37

    Account 4 65

    Using select Avg(Age) would return the int format.

  • You can first convert/cast the "age" value to a decimal, like this:

    if OBJECT_ID('tempdb..#Age') is not null

    drop table #Age

    create table #Age (Account int, Age int)

    insert into #Age

    values

    (1, 46),

    (2, 36),

    (3, 37),

    (4, 64)

    select

    AVG(CAST(Age as decimal(4,1))) as Average_Age

    from #Age

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Briceston (10/27/2016)


    Hello all,

    <snip>

    Data Age

    Account 1 46

    Account 2 36

    Account 3 37

    Account 4 65

    Using select Avg(Age) would return the int format.

    Btw: the given values average to a whole number ( 46+36+37+65 = 184 ... 184 / 4 = 46 ) :cool::w00t:

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • Thank you kind sir, you resolved my issue:)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply