Excluding certain fields in a SQL AVG field with a Case statement.

If you need to get an average of a field during a query, but exclude a few groups you can use a case statement inside of an AVG call to eliminate those groups from the average.

All that you need to do is return a null for the value in the case statement. For example the following code will get you the average for the amount field except where the name is Bob or John.

About the Author

Mike Griffith

Twitter Google+

A veteran programmer with over 20 years of experience in web development. If you have any questions please feel free to make a comment or send an email to mwgriffith@gmail.com.

Mike GriffithExcluding certain fields in a SQL AVG field with a Case statement.