我不确定“group by”的目的是什么这里

发布于 2024-12-09 08:11:17 字数 222 浏览 0 评论 0原文

我很难理解这个查询在做什么:

SELECT branch_name, count(distinct customer_name)
FROM depositor, account
WHERE depositor.account_number = account.account_number
GROUP BY branch_name

GROUP BY 需要什么?

I'm struggling to understand what this query is doing:

SELECT branch_name, count(distinct customer_name)
FROM depositor, account
WHERE depositor.account_number = account.account_number
GROUP BY branch_name

What's the need of GROUP BY?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

盗心人 2024-12-16 08:11:17

您必须使用 GROUP BY 才能以这种方式使用 COUNT 等聚合函数(使用聚合函数聚合与一个或多个值相对应的数据) em> 表内)。

该查询本质上是使用该列作为分组列来选择不同的branch_name,然后在组内对不同的customer_name进行计数。

如果没有 GROUP BY 子句(至少不是使用简单的查询规范 - 您可以使用其他方式,连接,子查询等...)。

You must use GROUP BY in order to use an aggregate function like COUNT in this manner (using an aggregate function to aggregate data corresponding to one or more values within the table).

The query essentially selects distinct branch_names using that column as the grouping column, then within the group it counts the distinct customer_names.

You couldn't use COUNT to get the number of distinct customer_names per branch_name without the GROUP BY clause (at least not with a simple query specification - you can use other means, joins, subqueries etc...).

桃扇骨 2024-12-16 08:11:17

它为您提供每个分支机构的不同客户总数; GROUP BY 用于分组 COUNT 函数。
也可以写成:

SELECT branch_name, count(distinct customer_name)
FROM depositor INNER JOIN account
ON depositor.account_number = account.account_number
GROUP BY branch_name

It's giving you the total distinct customers for each branch; GROUP BY is used for grouping COUNT function.
It could be written also as:

SELECT branch_name, count(distinct customer_name)
FROM depositor INNER JOIN account
ON depositor.account_number = account.account_number
GROUP BY branch_name
天冷不及心凉 2024-12-16 08:11:17

让我们暂时远离 SQL,看一下关系训练语言教程 D。

因为这两个关系(表)是在公共属性(列)名称 account_number 上连接的,所以我们可以使用自然连接:(

depositor JOIN account

因为结果是一个关系,根据定义它只有不同的元组(行) ),我们不需要 DISTINCT 关键字。)

现在我们只需要使用 SUMMARIZE..BY 进行聚合:

SUMMARIZE (depositor JOIN account) 
   BY { branch_name } 
      ADD ( COUNT ( customer_name ) AS customer_tally )

回到 SQLland,GROUP BYbranch_name 的作用与 SUMMARIZE..BY {branch_name} 相同。由于 SQL 具有非常严格的结构,因此 branch_name 列必须在 SELECT 子句中重复。

Let's take a step away from SQL for a moment at look at the relational trainging language Tutorial D.

Because the two relations (tables) are joined on the common attribute (column) name account_number, we can use a natural join:

depositor JOIN account

(Because the result is a relation, which by definition has only distinct tuples (rows), we don't need a DISTINCT keyword.)

Now we just need to aggregate using SUMMARIZE..BY:

SUMMARIZE (depositor JOIN account) 
   BY { branch_name } 
      ADD ( COUNT ( customer_name ) AS customer_tally )

Back in SQLland, the GROUP BY branch_name is doing the same as SUMMARIZE..BY { branch_name }. Because SQL has a very rigid structure, the branch_name column must be repeated in the SELECT clause.

浮云落日 2024-12-16 08:11:17

如果您想要COUNT某些内容(请参阅SELECT - 语句的一部分),则必须使用GROUP BY来告诉查询什么聚合。 GROUP BY 语句与聚合函数结合使用,可按一列或多列对结果集进行分组。

忽视它会导致大多数 RDBMS 中出现 SQL 错误,或者在其他 RDBMS 中导致毫无意义的结果。

有用的链接:
http://www.w3schools.com/sql/sql_groupby.asp

If you want to COUNT something (see SELECT-Part of the statement), you have to use GROUP BY in order to tell the query what to aggregate. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

Neglecting it will lead to SQL errors in most RDBMS, or senseless results in others.

Useful link:
http://www.w3schools.com/sql/sql_groupby.asp

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