Reply
Getting Averages of Averages
Old 09-01-2009, 10:39 AM Getting Averages of Averages
Skilled Talker

Posts: 64
Trades: 0
I run a site where people can rate articles written by members (user_id). These articles can then be rated by others on a special scale.

TABLE: articles -
id | user_id | title
1 | 3 | Transformers
2 | 5 | Shrek
3 | 2 | Shrek

TABLE: article_ratings -
article_id | rating_x | rating_y
1 | 4 | 5
2 | 3 | 2
2 | 4 | 4
3 | 2 | 5
2 | 5 | 3
3 | 4 | 4

Basically what I'm trying to do is get the average of the averages for a specific article. Example: I want to know the average of the average ratings for all the articles on 'Shrek'

The Shrek article ID's are 2 and 3

The average rating for Shrek with ID:2 are:
rating_x = 4
rating_y = 3

The average rating for Shrek with ID:3 are:
rating_x = 2
rating_y = 3

Then average those both together to bring a result of:
rating_x = 3
rating_y = 3
^^^^^^^^^^ And this result just here is the kind of output I want my database to provide.

I'm able to get the averages alone but am not able to get the average of the averages. I'm using PHP and SQL if that helps.

I'm thinking something along the lines of:
SELECT AVG(AVG(article_ratings.rating_x)), AVG(AVG(article_ratings.rating_y )) FROM article_ratings, articles WHERE articles.title='Shrek' AND articles_rating.article_id=articles.id
Obviously, two AVG()'s overlapping aren't allowed and my SQL isn't great as you can see so I hope someone can help.

Last edited by Petsmacker; 09-01-2009 at 02:39 PM..
Petsmacker is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 09-03-2009, 03:43 PM Re: Getting Averages of Averages
addonchat's Avatar
Super Talker

Posts: 113
Name: Chris Duerr
Trades: 0
I think I understand what you're looking for, but not entirely sure Your original math didn't add up to mine using your table data, but try this:

PHP Code:
SELECT AVG(avgx), AVG(avgyFROM 
      
(SELECT AVG(article_ratings.rating_x) AS avgxAVG(article_ratings.rating_y) AS avgyarticles.id AS id
         FROM article_ratings
articles 
         WHERE article_ratings
.article_id=articles.id AND articles.title='Shrek' 
         
GROUP BY articles.id) AS t1
Resulted in the following using your data:
PHP Code:
+------------+------------+
AVG(avgx)  | AVG(avgy)  |
+------------+------------+
3.50000000 3.75000000 |
+------------+------------+ 
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Old 09-03-2009, 08:02 PM Re: Getting Averages of Averages
Skilled Talker

Posts: 64
Trades: 0
If I could buy you a drink over the internet, I would. Seriously, fantastic. Thanks so much.
Petsmacker is offline
Reply With Quote
View Public Profile
 
Old 09-03-2009, 08:41 PM Re: Getting Averages of Averages
addonchat's Avatar
Super Talker

Posts: 113
Name: Chris Duerr
Trades: 0
You're very welcome! Best of luck to your web endeavor
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Getting Averages of Averages
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.16269 seconds with 13 queries