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
那么,如果你想取这个 sum 的平均值,只需做:
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
.
这篇关于一个查询中总和的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一个查询中总和的平均值


- 更改自动增量起始编号? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01