我有一个表
customer_purchases
,有3列:customer_id
,product_id
,quantity
,每个customer_id
可以购买多种不同数量的产品。
我需要找到最大购买数量(在所有购买的产品中)大于每个客户的平均数量的客户。我正在使用大查询。
我写了以下查询:
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
但我收到此错误:
每个函数参数都是一个表达式,而不是一个查询;要将查询用作表达式,必须使用附加括号将查询括起来,使其成为标量子查询表达式
这是什么意思?我该如何解决这个问题?
你想要更多这样的东西:
SELECT customer_id
FROM customer_purchases
group by customer_id
having sum(quantity) > avg( (select sum(quantity)
from `customer_purchases`
group by customer_id) )
order by customer_id