下面的相关子查询中是否需要GROUP BY?
给定场景:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下,可以省略
GROUP BY
,因为SELECT
列表中只有(一个)聚合函数,并且所有行都是保证属于同一个cust_id
组(通过WHERE
子句)。在这两种情况下,聚合将针对具有匹配 cust_id 的所有行。所以这两个查询都是正确的。
这将是
更干净实现相同功能的另一种方式:有一个区别:
(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 theSELECT
list and all rows are guaranteed to belong to the same group ofcust_id
( by theWHERE
clause).The aggregation will be over all rows with matching
cust_id
in both cases. So both queries are correct.This would be
a cleaneranother way to implement the same thing:There is one difference: rows with identical
(cust_id, amount)
infd
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 tableloan
, you get no rows at all. I assume you are aware of that.由于您按
cust_id
过滤数据,因此不需要GROUP BY
。在任何情况下,内部查询都会返回相同的结果。There are no need for
GROUP BY
since you filtered data bycust_id
. In any case inner query will return the same result.不,不是,因为您使用
id = fd.cust_id
计算客户的sum(amount)
,因此对于单个客户而言。但是,如果您的子查询以某种方式计算多个客户的总和,则
group by
将导致子查询生成多个行,这将导致条件 (<) 失败,因此,查询失败。No, it isn't, because you calculate
sum(amount)
for customer withid = 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.具有
sum
等聚合但没有group by
的查询将输出一组。将针对所有匹配行计算聚合。条件子句中的子查询只允许返回一行。如果子查询返回多行,下面的表达式意味着什么?
所以
group by
必须省略;您的第二个查询甚至会收到错误。注意:当您指定全部、任何或部分时,子查询可以返回多个rows:
但是很容易看出为什么这对你的情况没有意义;您会将一个客户的数据与另一客户的数据进行比较。
A query with an aggregate like
sum
but without agroup 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?
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:
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.