sql - 求总后的平均值
问题描述
我有疑问
SELECT language, COUNT(language) AS countx, sum(COUNT(language)) OVER() AS totalcount
FROM table1
where Language is not null
GROUP BY Language
结果
language countx totalcount
English 1672 1693
Spanish 21 1693
不知道如何从每组的 countx/totalcount 获得 AVG,所以 1672/1693 和 21/1693。
谢谢你。
解决方案
只需划分:
SELECT language, COUNT(language) AS countx,
sum(COUNT(language)) OVER () AS totalcount,
COUNT(language) * 1.0 / sum(COUNT(language)) OVER()
FROM table1
WHERE Language is not null
GROUP BY Language ;
一些数据库进行整数除法,这解释了* 1.0
.
推荐阅读
- sql-server - AWS Schema 转换工具 Microsoft SQL Server 驱动程序路径
- android - 什么是应用权限?我在寻找正确的信息吗?
- nearprotocol - NEAR 网络上有多少种不同类型的节点可用?
- python - Pandas、时间数据 ' ' 或空格与格式不匹配
- sql - 如何在 MariaDB 的“WHERE IN”子句中使用 JSON 数组
- reactjs - 与 Auth0 联合在 Amplify React 中不起作用
- android - Android 与 spinner 的一对多关系
- c# - WebAPI 异步任务
通过模型 - ruby - EnvelopesController#index 中的参数错误
- google-bigquery - Airflow Jinja 模板不适用于自定义运算符