下面的相关子查询中是否需要GROUP BY?

发布于 2024-12-23 01:48:13 字数 561 浏览 2 评论 0原文

给定场景:

table fd  
(cust_id, fd_id) primary-key and amount  

table loan  
(cust_id, l_id) primary-key and amount

我想列出所有定期存款金额低于所有贷款总和的客户。

查询:

SELECT cust_id
  FROM fd
    WHERE amount
     <
    (SELECT sum(amount)
        FROM loan
          WHERE fd.cust_id = loan.cust_id);

OR should we use

SELECT cust_id
  FROM fd
    WHERE amount
     <
    (SELECT sum(amount)
        FROM loan
          WHERE fd.cust_id = loan.cust_id group by cust_id);

一名客户可以拥有多笔贷款,但一次只考虑一项 FD。

Given scenario:

table fd  
(cust_id, fd_id) primary-key and amount  

table loan  
(cust_id, l_id) primary-key and amount

I want to list all customers who have a fixed deposit with an amount less than the sum of all their loans.

Query:

SELECT cust_id
  FROM fd
    WHERE amount
     <
    (SELECT sum(amount)
        FROM loan
          WHERE fd.cust_id = loan.cust_id);

OR should we use

SELECT cust_id
  FROM fd
    WHERE amount
     <
    (SELECT sum(amount)
        FROM loan
          WHERE fd.cust_id = loan.cust_id group by cust_id);

A customer can have multiple loans but one FD is considered at a time.

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

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

发布评论

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

评论(4

不必在意 2024-12-30 01:48:13

在这种情况下,可以省略 GROUP BY ,因为 SELECT 列表中只有(一个)聚合函数,并且所有行都是保证属于同一个 cust_id 组(通过​​ WHERE 子句)。

在这两种情况下,聚合将针对具有匹配 cust_id 的所有行。所以这两个查询都是正确的。


这将是更干净实现相同功能的另一种方式:

SELECT fd.cust_id
FROM   fd
JOIN   loan USING (cust_id)
GROUP  BY fd.cust_id, fd.amount
HAVING fd.amount < sum(loan.amount)

有一个区别(cust_id, amount)中的行具有相同的(cust_id, amount) >fd 在我的查询结果中只出现一次,而它们在原始查询中会出现多次。

无论哪种方式,如果表 loan 中没有非空 amount 的匹配行,您将根本没有行< /强>。我想你已经意识到这一点了。

GROUP BY can be omitted in this case, because there is only (one) aggregate function(s) in the SELECT list and all rows are guaranteed to belong to the same group of cust_id ( by the WHERE clause).

The aggregation will be over all rows with matching cust_id in both cases. So both queries are correct.


This would be a cleaner another way to implement the same thing:

SELECT fd.cust_id
FROM   fd
JOIN   loan USING (cust_id)
GROUP  BY fd.cust_id, fd.amount
HAVING fd.amount < sum(loan.amount)

There is one difference: rows with identical (cust_id, amount) in fd only appear once in the result of my query, while they would appear multiple times in the original.

Either way, if there is no matching row with a non-null amount in table loan, you get no rows at all. I assume you are aware of that.

淡墨 2024-12-30 01:48:13

由于您按 cust_id 过滤数据,因此不需要 GROUP BY。在任何情况下,内部查询都会返回相同的结果。

There are no need for GROUP BY since you filtered data by cust_id. In any case inner query will return the same result.

玩套路吗 2024-12-30 01:48:13

不,不是,因为您使用 id = fd.cust_id 计算客户的 sum(amount),因此对于单个客户而言。

但是,如果您的子查询以某种方式计算多个客户的总和,则 group by 将导致子查询生成多个行,这将导致条件 (<) 失败,因此,查询失败。

No, it isn't, because you calculate sum(amount) for customer with id = fd.cust_id, so for a single customer.

However, if somehow your subquery calculate sum for more than one customer, the group by would cause the subquery to generate more than one row and this will cause the condition(<) to fail, and thus, the query to fail.

哑剧 2024-12-30 01:48:13

具有 sum 等聚合但没有 group by 的查询将输出一组。将针对所有匹配行计算聚合。

条件子句中的子查询只允许返回一行。如果子查询返回多行,下面的表达式意味着什么?

where 1 > (... subquery ...)

所以group by必须省略;您的第二个查询甚至会收到错误。

注意:当您指定全部、任何或部分时,子查询可以返回多个rows:

where 1 > ALL (... subquery ...)

但是很容易看出为什么这对你的情况没有意义;您会将一个客户的数据与另一客户的数据进行比较。

A query with an aggregate like sum but without a group by will output one group. The aggregates will be computed over all matching rows.

A subquery in a condition clause is only allowed to return one row. If the subquery returned multiple rows, what would the following expression mean?

where 1 > (... subquery ...)

So the group by must be omitted; you would even get an error for your second query.

N.B. When you specify all, any, or some a subquery can return multiple rows:

where 1 > ALL (... subquery ...)

But it's easy to see why that doesn't make sense in your case; you'd compare one customer's data to that of another.

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