Query for average without counting zeros

I have a string in my form like this.

Sub SumAsking_Rent_AVERAGE()
   Me.Asking_Rent_Average = (Nz(Me.Asking_Rent_LOW, 0) + Nz(Me.Asking_Rent_HIGH, 0)) / 2
End Sub

This basically adds two fields and puts the average in the form field.

Now I need to create a query that will add the table column data and give me an average without adding the zeros.  I had to put a default zero in the cell.  Right no my "test" SQL looks like this.


SELECT DISTINCTROW TradeArea.TradeAreaID, Count(*) AS [Count Of Property], Avg(Property.AskingRentAVERAGE) AS AvgOfAskingRentAVERAGE
FROM Property 
GROUP BY TradeArea.TradeAreaID
HAVING (((TradeArea.TradeAreaID)=1));

Open in new window

napsternovaDeveloperAsked:
Who is Participating?
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
try this

SELECT Sum(Property.AskingRentAVERAGE) AS TSum, Sum(IIf([AskingRentAVERAGE]>0,1,0)) AS vCount, [Tsum]/[vCount] AS Average
FROM Property
0
 
carsRSTCommented:
SELECT DISTINCTROW TradeArea.TradeAreaID, Count(*) AS [Count Of Property], Avg(Property.AskingRentAVERAGE) AS AvgOfAskingRentAVERAGE
FROM Property
GROUP BY TradeArea.TradeAreaID
HAVING TradeArea.TradeAreaID=1 and Avg(Property.AskingRentAVERAGE) >0
0
 
napsternovaDeveloperAuthor Commented:
I tried that but I am getting crazy small numbers for my average.  I should have an average of $25 but am getting $1.32.  It is dividing by the row count.  In the database itself if I filter out the zeros I get the correct average. ??
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Rey Obrero (Capricorn1)Commented:
which query are you referring to?
0
 
carsRSTCommented:
Might try this....

SELECT TradeArea.TradeAreaID, Count(*) AS [Count Of Property], Avg(Property.AskingRentAVERAGE) AS AvgOfAskingRentAVERAGE
FROM
(
SELECT * FROM Property where AskingRentAVERAGE <> 0 as table1)

)
where TradeAreaID=1
GROUP BY TradeArea.TradeAreaID
0
 
napsternovaDeveloperAuthor Commented:
Sorry for the confusion.  Capricorn must have submitted as I was typing.  I was referring to the first response by carsRST.  I will try both of your solutions. Thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.