Solved

compute avg from a result set

Posted on 2006-10-23
6
964 Views
Last Modified: 2008-02-01
I have an SQL statement

select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3 from table;

now I would like to compute avg(result1,result2,result3)

But how? Can it be done within MySQL?
0
Comment
Question by:ladwein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17787509
you mean like this:
select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3
, coalesce(avg(col1 + col2 + col3 ),0)  as avg_result
from table;

0
 
LVL 1

Author Comment

by:ladwein
ID: 17787585
This returns (for example)

result1 = 1.6863
result2 = 1.6471
result3 = 1.7379
avg_result = 5.0909

avg_result should be 1.6904 which is (result1 + result2 + result3)/3 ;if none of the resultx is NULL

I have tried to do it in Java but then getDouble() returns 0.0 for NULL values which is incorrect since I also have 0.0 as a result

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 17787722
hi

try doing this :
select avg(result1 + result2 + result3)/3 as result from
( select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3
from table) as t


momi sabag
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 6

Expert Comment

by:hans_vd
ID: 17787932
I think AngelIII just forgot to put '/3':

select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3
, coalesce(avg((col1 + col2 + col3) / 3 ),0)  as avg_result
from table;
0
 
LVL 14

Expert Comment

by:racek
ID: 17787943
select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3,
SUM(coalesce(avg(col1),0) + coalesce(avg(col2),0) + coalesce(avg(col3),0)) / (COUNT(col1) + COUNT(col2) + COUNT(col3))  as avg_result
from table;
0
 
LVL 14

Accepted Solution

by:
racek earned 125 total points
ID: 17787967
I don't count columns with NULL value into AVG

COL1   COL2  COL3
2          NULL   2
2          NULL   NULL
2            6      4
----------------------------
AVG
2            6      3
----------------------------
Total AVG = 18 / 6

select coalesce(avg(col1),0) as result1, coalesce(avg(col2),0) as result2, coalesce(avg(col3),0) as result3,
SUM(coalesce(col1,0) + coalesce(col2,0) + coalesce(col3,0)) / (COUNT(col1) + COUNT(col2) + COUNT(col3))  as avg_result
from table;
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CLI command keep running after close 7 73
reverse engineer .sql from php files 11 65
MySQL recovery 7 58
emailing registered email addresses in phpmyadmin 5 67
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question