How do I pass the greater than condition? I'm getting an error as invalid column name 'average'.
Here is my code:
SELECT P.prod_cat, AVG(total_amt) AS average
FROM Transactions T JOIN
prod_cat_info P
ON T.prod_cat_code = P.prod_cat_code
WHERE average > AVG(total_amt)
GROUP BY prod_cat
4条答案
按热度按时间jum4pzuy1#
Based on the title to your question, I think you want:
yeotifhr2#
Since you're trying to compare all of the rows to a single value you should make sure that you're only getting that single value once. The following code had decent performance in one of my larger local databases (table names have been changed to protect the innocent):
ioekq8ef3#
y4ekin9u4#
What if Quantity and Amount columns are given separately? In that case we need to follow a different way to calculate the overall average and the Category-wise average. I have used CTE below which makes the code look very simple.
SELECT * FROM CTE_Avg_Cat_Revenue, CTE_Overall_Avg WHERE CTE_Avg_Cat_Revenue.Cat_Avg > CTE_Overall_Avg.Overall_Average;