Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

compute avg from a result set

Posted on 2006-10-23
6
Medium Priority
?
980 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
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

886 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