不可能对子查询的结果取平均值吗?
这是我要解决的查询:
SELECT AVG(
SELECT SUM(`retail_subtotal`)
FROM `order`
WHERE `status` IN('O')
GROUP BY `lead_id`
);
最佳答案
实际上,一种简单的查询短语方式是没有子查询:
SELECT SUM(`retail_subtotal`)/count(distinct lead_id) as avg
FROM `order`
WHERE `status` IN ('O')
(这假设lead_id永远不会为NULL。)
您的原始查询有一个问题,这不仅是因为avg()中的子查询,还因为该子查询返回了多行。