Query Avg / Ignore Null Values and Zeros

  • Thread starter Thread starter gumby
  • Start date Start date
G

gumby

I would like my Avg(Calls.TotalCumScore) to ignore Null Values and
Zeros.



SELECT Calls.Monitor, Avg(Calls.TotalCumScore) AS AvgOfTotalCumScore,
Count(Calls.Completed) AS CountOfCompleted
FROM Calls
GROUP BY Calls.Monitor;

I think it is already ignoring the null values, but how do i get it to
ignore the Zeros?


David
 
Hi,


NULL are automatically disregarded.


Add a WHERE clause to remove the zeros.


SELECT Calls.Monitor, Avg(Calls.TotalCumScore) AS AvgOfTotalCumScore,
Count(Calls.Completed) AS CountOfCompleted
FROM Calls
WHERE TotalCumScore <> 0
GROUP BY Calls.Monitor;


The WHERE clause is evaluated BEFORE the aggregates, while the HAVING clause
is evaluated AFTER the aggregates. So, here, simply a WHERE clause will do,
since you want to remove the records before computing the average.


Hoping it may help,
Vanderghast, Access MVP
 
SELECT Calls.Monitor, Avg(Calls.TotalCumScore) AS AvgOfTotalCumScore,
Count(Calls.Completed) AS CountOfCompleted
FROM Calls
GROUP BY Calls.Monitor
HAVING Calls.TotalCumScore IS NOT NULL AND Calls.TotalCumScore <> 0;
 
The solution depends on whether or not your count and average are
independent of each other. Do you want to count Call.Completed if the
TotalCumScore is null or zero? IF not then

SELECT Calls.Monitor
, Avg(Calls.TotalCumScore) AS AvgOfTotalCumScore
, Count(Calls.Completed) AS CountOfCompleted
FROM Calls
WHERE TotalCumScore <> 0 and TotalCumScore is Not Null
GROUP BY Calls.Monitor

If the two are independent of each other then

SELECT Calls.Monitor
, Avg(IIF(Calls.TotalCumScore) = 0,Null, TotalCumScore)) AS
AvgOfTotalCumScore
, Count(Calls.Completed) AS CountOfCompleted
FROM Calls
GROUP BY Calls.Monitor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top