October 27, 2016 at 12:20 am
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.
October 27, 2016 at 12:52 am
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
October 27, 2016 at 12:57 am
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:
October 27, 2016 at 7:16 am
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