How does GROUP BY work?(GROUP BY 如何工作?)
问题描述
假设我有一个带有属性的表 Tab1
- a1
、a2
等等.没有一个属性是唯一的.
Suppose I have a table Tab1
with attributes - a1
, a2
, ... etc. None of the attributes are unique.
以下查询的性质是什么?它会始终返回一行吗?
What will be the nature of the following query? Will it return a single row always?
SELECT a1, a2, sum(a3) FROM Tab1 GROUP BY a1, a2
推荐答案
GROUP BY
为 GROUP BY
字段的每个唯一组合返回一行.因此,在您的示例中, (a1, a2)
出现在 Tab1
行中的每个不同组合都会导致查询中的一行代表具有给定组合的行组按字段值分组.SUM()
等聚合函数是针对每个组的成员计算的.
GROUP BY
returns a single row for each unique combination of the GROUP BY
fields. So in your example, every distinct combination of (a1, a2)
occurring in rows of Tab1
results in a row in the query representing the group of rows with the given combination of group by field values . Aggregate functions like SUM()
are computed over the members of each group.
这篇关于GROUP BY 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GROUP BY 如何工作?


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