Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Avg

Author  Topic 

laddu
Constraint Violating Yak Guru

332 Posts

Posted - 2011-06-06 : 11:03:07
Hi,

I am trying to pull avg for TotalMessages, Totalread and Totalunread.
Using below script I am not getting decimals.

Can some one help me to get accurate avg with decimals. Thank you.




Select

@accountID as AccountID,

AVG(Totalmessages),
ROUND(SUM(ISNULL(TotalMessages,0)) / COUNT(*),3) as TotalAvgMessagesPerPatient,
ROUND(SUM(ISNULL(TotalUnread,0)) / COUNT(*),3) as TotalAvgUnreadMessagesPerPatient,
ROUND(SUM(ISNULL(TotalRead,0)) / COUNT(*),3) as TotalAvgReadMessagesPerPatient

from #temp_patients





sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-06 : 12:38:54
In most cases AVG function returns the same data type as that of the column being averaged (http://msdn.microsoft.com/en-us/library/ms177677.aspx). So you may need to cast the values to the desired result type and then average, for example:

.....
AVG(cast(Totalmessages as decimal(19,4))),
.....
Go to Top of Page
   

- Advertisement -