Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return average for each tow

Status
Not open for further replies.

hapax

Programmer
Nov 10, 2006
105
US
I'm a SQL newbie.

How can I return the average of a column for each row in a table?

Here is my Comments table - it's ratings and comments about games, one user comment per row. How can I compute the average Rating for each GameID?

CREATE TABLE [Comments](
[CommentID] [int]
[GameID] [int]
[UserID] [varchar](50)
[Comment] [varchar](2500)
[Rating] [int]
)



Thanks
 
Something like this ?
SELECT GameID, AVG(Rating)
FROM Comments
GROUP BY GameID

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This returns whole integers at the averages. Is that correct? Does SQL round the results with the AVG function?

 
AVG returns the same data type as the parameter. Feed it an integer and it will return an integer. You can convert the integer to a decimal data type, and then the average will return a decimal data type. Like this.

Code:
SELECT GameID, AVG(Rating [!]* 1.0[/!])
FROM Comments
GROUP BY GameID

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top