Is it possible to use Aggregate function in a Select statment without using Group By clause?(是否可以在 Select 语句中使用 Aggregate 函数而不使用 Group By 子句?)
问题描述
到目前为止,我已经编写了聚合函数,然后是 Group By 子句,以查找基于 SUM、AVG 和其他聚合函数的值.我对 Group By 子句有点困惑.当我们使用聚合函数时,我需要在 Group By 子句中指定哪些列.否则有什么方法可以不使用 Group By 子句来使用聚合函数.
So far I have written Aggregate function followed by Group By clause to find the values based on SUM, AVG and other Aggregate functions. I have a bit confusion in the Group By clause. When we use Aggregate functions what are the columns I need to specify in the Group By clause. Otherwise Is there any way to use Aggregate functions without using Group By clause.
推荐答案
SELECT 子句中所有没有聚合的列都需要在 GROUP BY 中
All columns in the SELECT clause that do not have an aggregate need to be in the GROUP BY
好:
SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3
也不错:
SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3, col5, col6
没有其他列 = 不需要 GROUP BY
No other columns = no GROUP BY needed
SELECT MAX(col4)
...
不起作用:
SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2
毫无意义:
SELECT col1, col2, col3, MAX(col4)
...
GROUP BY col1, col2, col3, MAX(col4)
在没有 GROUP BY 的情况下对其他列进行聚合(MAX 等)是没有意义的,因为查询变得不明确.
Having an aggregate (MAX etc) with other columns without a GROUP BY makes no sense because the query becomes ambiguous.
这篇关于是否可以在 Select 语句中使用 Aggregate 函数而不使用 Group By 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以在 Select 语句中使用 Aggregate 函数而不使用 Group By 子句?


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