If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Single average value from two tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-23-04, 08:23
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Single average value from two tables

Hi,

I have two tables containing scores and I would like to obtain a single average score from those two tables. Is there a way to do that?

I've the sql below as follows:

Code:
sql statement
SELECT COUNT(*) AS count1, AVG(score) AS avg_score1
      FROM scores1
      WHERE id=3
      UNION ALL SELECT COUNT(*) AS count2, AVG(score) AS avg_score2
      FROM scores2
      WHERE id=3

Sample table values

Table scores1
id score
3  100
3   80

Table scores2
id score
3   50
3   80
3   80
That got me the results as follows:

Code:
Count average
2        90
3        70
I was looking for a way to get the average of all the five scores. In order words, I would like the query to produce the average score of 78 (sum of all the test scores divided by 5).

Is there a way to that?

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 12-25-04, 09:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
requires 4.1 --
Code:
select count(*)   as count_both
     , avg(score) as avg_score_both
  from (
       select score
         from scores1
        where id = 3     
       union all
       select score
         from scores2
        where id = 3   
       ) as dt
or look up merge tables at mysql.com
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-25-04, 21:33
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Thanks, r937

Both my development and production servers are using MySQL 4.0.xx. I can't upgrade to 4.1.xx in the production server because it's a paid share server which I've no control over.

Will come back to this solution when the MySQL server is upgraded.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On