1

在 Brackendale 分行查找所有贷款大于所有贷款的客户的姓名。

这个可以吗?

Use University;

SELECT customer_name, loan_number
FROM loan NATURAL JOIN customer
HAVING MAX(amount) > ALL (SELECT sum(loan.amount)
FROM loan 
WHERE branch_name = 'Brakendale');
4

2 回答 2

3

ALL是一个不需要的关键字 - 它什么也不做,因为它是集合的默认关键字(另一个关键字是ANY)。

省略更直接ALL。但是你可能想要一个 group by 并删除loan_number,否则每一行都必须在它自己的组中。

这个查询对我来说似乎更有意义:

SELECT customer_name, MAX(amount)
FROM loan
NATURAL JOIN customer
GROUP BY customer_name
HAVING MAX(amount) > (
  SELECT sum(loan.amount)
  FROM loan 
  WHERE branch_name = 'Brakendale');
于 2012-12-27T19:40:25.873 回答
0

您可能想要MAX而不是SUM相反:

SELECT customer_name, loan_number
FROM loan NATURAL JOIN customer
HAVING SUM(amount) > (SELECT max(loan.amount)
                      FROM loan 
                      WHERE branch_name = 'Brakendale'
                     );

这将是“所有总贷款大于布雷肯代尔最大贷款的客户”。您的查询更像是“所有贷款总额大于布雷肯代尔所有未偿贷款总和的客户”。

您甚至可能想要“所有未偿还贷款大于布雷肯代尔任何客户的客户”。这将是:

SELECT customer_name, loan_number
FROM loan NATURAL JOIN customer
HAVING SUM(amount) > (SELECT max(l.CustomerLoans)
                      from (select customerID, sum(l.amount) as CustomerLoans
                            FROM loan l
                            WHERE branch_name = 'Brakendale'
                            group by customerID
                           ) l
                     );
于 2012-12-27T20:25:56.103 回答