?
Solved

ms sql server 2005 non zero avg query

Posted on 2011-09-06
6
Medium Priority
?
512 Views
Last Modified: 2012-06-27
I have the following table named Complaints

shop  complaint1  complaint2  complaint3
1200  0                 5                 10


I am trying to write a query that will avg the non zero fields


Using the above example I would like 7.5 to be returned

is there an easy way to do this?
0
Comment
Question by:johnnyg123
[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 29

Expert Comment

by:sammySeltzer
ID: 36491777
select avg(yourfield) from yourtable where yourfield <> 0
0
 

Author Comment

by:johnnyg123
ID: 36491830
I guess my example wasn't the greatest.  

I'm not looking to avg a single field

there are 3 different fields  (complaint1, complaint2 and complaint3) which I want to add up and get the everage of (but only if it is non zero)  If all  3 fields are 0 I want the average to be 0

if complaint1 is 2,   but complaint2 is 0 and complaint3 is 0


I am looking to get an average of 2
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 36491846
you can't avg() across multiple fields like that.  You have to sum and divide by the number of non-zero values.

example:

CREATE TABLE #test (a INT,b INT,c INT)
INSERT INTO #test( a, b, c )
VALUES ( 0,5,10)
go
SELECT SUM(a+b+c) / (SUM(CASE WHEN a=0 THEN 0 ELSE 1 END + CASE WHEN b=0 THEN 0 ELSE 1 END + CASE WHEN c=0 THEN 0 ELSE 1 end)*1.0)
FROM #test t
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36491848
select

cast((isnull(column1,0) + isnull(column2,0) + isnull(column3,0)) as decimal(10,2)) /
((case when isnull(column1,0) > 0 then 1 else 0 end) + (case when isnull(column2,0) > 0 then 1 else 0 end) + (case when isnull(column3,0) > 0 then 1 else 0 end))
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 2000 total points
ID: 36495013
Another thought on my post.  If all of the columns are 0, you'll run into a divide by 0 error.  You'll need to wrap the divisor with a  NULLIF() function, which will turn the 0 to null thus preventing the error.

select

cast((isnull(column1,0) + isnull(column2,0) + isnull(column3,0)) as decimal(10,2)) /
nullif(((case when isnull(column1,0) > 0 then 1 else 0 end) + (case when isnull(column2,0) > 0 then 1 else 0 end) + (case when isnull(column3,0) > 0 then 1 else 0 end)),0)
0
 

Author Closing Comment

by:johnnyg123
ID: 36495140
Thaniks

I wrapped an isnull around the whole statement so that if all 3 values were null that it would return 0 instead of null

    ISNULL(CAST(ISNULL(SUM(ComplaintM1), 0) + ISNULL(SUM(Complaintm2), 0) + ISNULL(SUM(Complaintm3), 0) AS decimal(10, 2))
                      / NULLIF ((CASE WHEN isnull(SUM(ComplaintM1), 0) > 0 THEN 1 ELSE 0 END) + (CASE WHEN isnull(SUM(ComplaintM2), 0) > 0 THEN 1 ELSE 0 END)
                      + (CASE WHEN isnull(SUM(ComplaintM3), 0) > 0 THEN 1 ELSE 0 END), 0), 0)
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
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…

800 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