如何从MySQL中获取平均值的最大值?以下查询返回按客户分组的表格订单的金额平均值。
SELECT AVG(amount)
FROM orders
GROUP BY cust;
我希望使用带有聚合函数的单个查询从平均值中获取最大值。使用ORDER BY ... DESC LIMIT 1肯定有效,但我感兴趣的是仅使用聚合函数获得最大平均值。有可能吗?感谢
答案 0 :(得分:4)
<ul class="ch-grid">
<li>
<div class="ch-item ch-img-1">
<div class="ch-info">
<h3>Games</h3>
<p>
by Bill Hicks <a href="http://www.iristimes.com">Quickly! go there now</a>
</p>
</div>
</div>
</li>
<li>
<div class="ch-item ch-img-2">
<div class="ch-info">
<h3>Illustration</h3>
<p>
by Steven Wright <a href="http://www.independent.ie ">Captain Semantics</a>
</p>
</div>
</div>
</li>
<li>
<div class="ch-item ch-img-3">
<div class="ch-info">
<h3>Characters</h3>
<p>
by Johnny Vegas <a href="http://www.examiner.ie ">Lovable Mentalist</a>
</p>
</div>
</div>
</li>
<li>
<div class="ch-item ch-img-4">
<div class="ch-info">
<h3>Modelling</h3>
<p>
by Benedict Cumberbatch <a href="http://www.dribbble.com">Smartypants Himself</a>
</p>
</div>
</div>
</li>
</ul>
答案 1 :(得分:2)
我会使用order by
和limit
执行此操作:
SELECT AVG(o.amount) as avg_value
FROM orders o
GROUP BY cust
ORDER BY avg_value DESC
LIMIT 1;
这样您也可以获得最大值cust
。