Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Avg

Status
Not open for further replies.

at51178

Technical User
Mar 25, 2002
587
0
0
US
Hey Guys

I would like to avg out a column in a query but I want the computer to not see the columns that at 0 so for example

you have (0 + 1+ 2 + 3)/3 "This is basically what I want the computer to do but it is doing this (0+1+2+3)/4 it is adding the row with the 0 in the computation

any help can be appreciated
 
Try this SQL:

Select sum(col) / (Select count(*) from table where col<>0)
from table;
 
If you want to remove the 0 and the column then make the column null. If this is an Access MDB then use the iif statement, sql server has the nullif() function. By making the column null it will be effectively removed from the computation.

iif(col1=0,null,col1)

iif(expression,true,false) 'please double check syntax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top