I have a table customer_purchases with 3 columns: customer_id, product_id, quantity, where each customer_id can buy multiple products having different quantities.
I need to find customers whose maximum quantity bought (among all the products purchased) is greater than average quantity of every/each customer. I am using Big Query.
I wrote the following query :
SELECT customer_id
FROM customer_purchases
GROUP BY customer_id
HAVING MAX(quantity) > MAX(SELECT AVG(quantity)
FROM `farmers_market.customer_purchases`
GROUP BY customer_id)
ORDER BY customer_id
But I get this error:
Each function argument is an expression, not a query; to use a query as an expression, the query must be wrapped with additional parentheses to make it a scalar subquery expression
What does this mean, and how do I resolve this problem?
You want something more like this:
Note the extra set of parentheses inside the
avg()function. Also noticed that I changed which aggregate functions you were using, since the original functions were not correct for the desired results.