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 Programming
 returning a floating point average

Author  Topic 

pierznj
Starting Member

8 Posts

Posted - 2008-01-20 : 18:40:34
Hi SQL people,
I have rating system on pages in my website, each page being rated one to five by users. At the back end, an ASP.NET page displays the average rating and number of ratings for each page. As the rating is stored as an integer, the SQL statement returns an integer average. I would like to get a floating point average. Currently the statement used to return the average looks like this:

select AVG(Rating) as AverageRating from Ratings where [RatingPage] = @RatingPage

Is there a simple way to modify this to return a floating point average without iterating through the records, or converting the Rating field to a float?

Thanks for helping!

nr
SQLTeam MVY

12543 Posts

Posted - 2008-01-20 : 20:24:34
select AVG(convert(float,Rating))

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-01-20 : 20:28:18
Try this, replace n and m to whatever you need:

select AVG(convert(decimal(n, m), Rating) as AverageRating from Ratings where [RatingPage] = @RatingPage
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-21 : 09:31:45
or

select AVG(Rating*1.0) as AverageRating from Ratings where [RatingPage] = @RatingPage

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -