SQL 中 group by 语句的根本目的是什么?

发布于 2025-01-11 17:56:30 字数 1050 浏览 0 评论 0原文

最近,我一直在处理对数据执行大量转换的极其广泛的查询,并且我对必须维护广泛的group by语句感到恼火。这让我想知道,

为什么它们存在?

例如

select 
    company, 
    sum(owing) as owing
from 
    receivables
group by 
    company

,鉴于这个陈述,在我看来,group by是隐含的。

  1. 有一个聚合函数
  2. ,只有 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.

  1. There is an aggregate function
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2025-01-18 17:56:30

Oracle 支持嵌套聚合函数的示例

假设您有一个多维数据集滚动结果。
以下查询向我们显示了抛出分布。

select   result
        ,count(*) as count
from     cube_roll
group by result 
RESULTCOUNT
111
223
312
423
515
616

以下查询显示结果的最大计数。
请注意 result 不会出现在 SELECT 子句中。

select   max(count(*)) as max_count
from     cube_roll
group by result 
MAX_COUNT
23

请注意,结果不能添加到 SELECT 子句中。

select   result        -- invalid reference
        ,max(count(*)) as max_count
from     cube_roll
group by result 

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.

select   result
        ,count(*) as count
from     cube_roll
group by result 
RESULTCOUNT
111
223
312
423
515
616

The following query shows us the maximum count for the results.
Please note that result does not appear in the SELECT clause.

select   max(count(*)) as max_count
from     cube_roll
group by result 
MAX_COUNT
23

Please note that result cannot be added to the SELECT clause.

select   result        -- invalid reference
        ,max(count(*)) as max_count
from     cube_roll
group by result 

ORA-00937: not a single-group group function

Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文