一个查询的平均和 [英] Avg of a Sum in one query
本文介绍了一个查询的平均和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道我是否可以在一个SQL SERVER请求中获得总和的平均值,
I would like to know if I can get the average of a sum in one single SQL SERVER request,
尝试使用以下请求执行此操作,但是它不起作用:
Have tried to do it with the following request but it doesn't work:
SELECT t.client,
AVG(SUM(t.asset)) AS Expr1
FROM TABLE t
GROUP BY t.client
推荐答案
我认为您的问题需要一些解释.如果要采用t.client
分组的总和,则可以使用:
I think your question needs a bit of explanation. If you want to take the sums grouped by t.client
you can use:
SELECT t.client, SUM(t.asset)
FROM the-table t
GROUP BY t.client
然后,如果要取该总和的平均值,则只需进行以下操作即可:
Then, if you want to take the average of this sume, just make:
SELECT AVG(asset_sums)
FROM
(
SELECT t.client, SUM(t.asset) AS asset_sums
FROM the-table t
GROUP BY t.client
) as inner_query
但是您不能对外部查询进行分组,因为这将为您提供类似于第一个查询的结果.内部查询的结果已按t.client
分组.
You can't however group the outer query, because this will give you results like in the first query. The results from the inner query are already grouped by t.client
.
这篇关于一个查询的平均和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文