Skip to content
Advertisement

SQL – An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause

If you see the picture below, I only want to see those categories where the average revenue is greater than the overall average.

enter image description here

The query that I’m using is below. The very last line is causing the issue.

The error that I’m getting is:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

Please let me know how I can fix this. Thanks!

Advertisement

Answer

I think the query you want to write is something like:

The subquery aggregates transactions by category, compute the average, and compares it against the overall transaction average. Then we just bring the categories table with a join.

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