Skip to content
Advertisement

Use average of values in “where” clause

I want to select the top x value from a query where the average of valid values for a particular field is above 10. Why is this throwing an error?

Advertisement

Answer

Your code is throwing an error because avg() is an aggregation function and cannot be in the where clause.

What you want seem to want is apply:

The subquery calculates the average. That calculation can be used in the WHERE clause.

Note that the comparison to NULL is unnecessary, because > filters out NULL values.

Also, this returns the average. If you don’t want the average in the results, just select A.* instead of *.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement