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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join, with an average too

Status
Not open for further replies.

hapax

Programmer
Nov 10, 2006
105
US
I'm trying to return all the fields from one table, and also the average of a column in another table.

I think the following SQL pretty much shows what I'm trying to accomplish, but it's not working yet:

SELECT Games.*, AVG(Comments.Rating)
FROM Games, Comments
Where Games.GameID = Comments.GameID
GROUP BY Games.GameName
ORDER BY Games.MinAge, Games.BggRating DESC


Please help. I'm a SQL newbie.

Thanks
 
One way:
SELECT G.GameID, G.GameName, G.MinAge, G.BggRating, G.AnotherCol, AVG(C.Rating)
FROM Games G INNER JOIN Comments C ON G.GameID = C.GameID
GROUP BY G.GameID, G.GameName, G.MinAge, G.BggRating, G.AnotherCol
ORDER BY G.MinAge, G.BggRating DESC

Another way:
SELECT G.*, (SELECT AVG(Rating) FROM Comments WHERE GameID = G.GameID)
FROM Games G
ORDER BY G.MinAge, G.BggRating DESC

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top