SQL 中 group by 语句的根本目的是什么?
最近,我一直在处理对数据执行大量转换的极其广泛的查询,并且我对必须维护广泛的group by
语句感到恼火。这让我想知道,
为什么它们存在?
例如
select
company,
sum(owing) as owing
from
receivables
group by
company
,鉴于这个陈述,在我看来,group by
是隐含的。
- 有一个聚合函数
- ,只有
company
不是聚合的一部分。
因此,我希望查询引擎可以确定 company
应该是分组的对象。
select
company,
sum(owing) as owing
from
receivables
我的一般假设总是这样的事情存在是有原因的,我只是不明白原因,但是......我不明白原因。
什么场景下需要group by
的存在?
更新
根据评论,关于多表查询的一点对于引擎来说不太明显。另外,关于多个非聚合字段的一点。
select
c.name as company,
t.curr as currency,
sum(t.amt) as owing
from
company c
inner join transactions t on c.id = t.comp_id
having
sum(t.amt) < 0
原始查询的这个(更现实的)版本使用两个表。我仍然不清楚为什么引擎不知道对公司和货币进行分组,因为它们是非聚合字段
Lately I have been dealing with extremely wide queries that perform a lot of transforms on data, and I am annoyed by having to maintain wide group by
statements. This has me wondering,
why do they exist?
For example
select
company,
sum(owing) as owing
from
receivables
group by
company
Given this statement, it seems to me that the group by
is implied.
- There is an aggregate function
- There only field not part of an aggregation is
company
.
Therefore, I would expect that a query engine could determine that company
should be the thing grouped on.
select
company,
sum(owing) as owing
from
receivables
My general assumption is always that something like this exists for a reason, I just don't understand the reason, but ... I don't understand the reason.
What is the scenario that makes the existence of group by
necessary?
Update
Based on comments, a point regarding mult-table queries making it less obvious to the engine. Also, a point regarding multi-nonaggregate fields.
select
c.name as company,
t.curr as currency,
sum(t.amt) as owing
from
company c
inner join transactions t on c.id = t.comp_id
having
sum(t.amt) < 0
This (more realistic) version of the original query uses two tables. It is still unclear to me, why the engine would not know to group on company
and currency
as they are the non-aggregated fields
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Oracle 支持嵌套聚合函数的示例
假设您有一个多维数据集滚动结果。
以下查询向我们显示了抛出分布。
以下查询显示结果的最大计数。
请注意
result
不会出现在 SELECT 子句中。请注意,
结果
不能添加到 SELECT 子句中。ORA-00937: 不是单组组函数
Fiddle
An example from Oracle which supports nested aggregate functions
Assume that you have a cube rolling results.
The following query shows us the throws distribution.
The following query shows us the maximum count for the results.
Please note that
result
does not appear in the SELECT clause.Please note that
result
cannot be added to the SELECT clause.ORA-00937: not a single-group group function
Fiddle