我有一个银行数据库:
我想获取特定分支机构的客户名称和平均余额。也就是说,他们的储蓄帐户和支票帐户在“ x”分支的平均值。
我尝试了以下查询,但未成功。
我有这个查询:
SELECT DISTINCT customer_name, average_balance FROM
(
SELECT account_number, AVG(balance) AS average_balance
FROM (
SELECT account_number, balance FROM account WHERE account_number IN (SELECT account_number FROM savings_account)
UNION ALL
SELECT account_number, balance FROM account WHERE account_number IN (SELECT account_number FROM checking_account)
) AS subquery
GROUP BY account_number
) AS subquery2 INNER JOIN customer;
示例输入语句:
INSERT INTO account VALUES ('ACC-101', 'Rock Ridge', 500000, '');
INSERT INTO account VALUES ('ACC-101', 'Beach Lookout', 1700, '');
INSERT INTO account VALUES ('ACC-102', 'Rock Ridge', 2465, '');
INSERT INTO account VALUES ('ACC-103', 'Rock Ridge', 77350, '');
INSERT INTO account VALUES ('ACC-104', 'Bargin Plaza', 480002, '');
INSERT INTO account VALUES ('ACC-105', 'Supreme Hill', 17000, '');
INSERT INTO account VALUES ('ACC-106', 'Rock Ridge', 5000, '');
INSERT INTO account VALUES ('ACC-107', 'Main Sperem', 3000, '');
INSERT INTO account VALUES ('ACC-108', 'Adus Avenue', 57685, '');
INSERT INTO savings_account VALUES('ACC-101', 0.1);
INSERT INTO savings_account VALUES('ACC-102', 0.7);
INSERT INTO savings_account VALUES('ACC-103',0.6);
INSERT INTO checking_account VALUES('ACC-104', 2000);
INSERT INTO checking_account VALUES('ACC-105', 2000);
INSERT INTO checking_account VALUES('ACC-106', 2000);
INSERT INTO depositor VALUES ('125-233-001', 'ACC-101', '2012/10/13');
INSERT INTO depositor VALUES ('125-233-001', 'ACC-106', '2013/09/21');
INSERT INTO depositor VALUES ('125-233-002', 'ACC-102', '2014/08/11');
INSERT INTO depositor VALUES ('125-233-002', 'ACC-106', '2015/07/06');
INSERT INTO depositor VALUES ('125-233-004', 'ACC-104', '2016/06/11');
INSERT INTO depositor VALUES ('125-233-005', 'ACC-105', '2012/05/17');
INSERT INTO depositor VALUES ('125-233-006', 'ACC-105', '2018/10/11');
INSERT INTO depositor VALUES ('125-233-007', 'ACC-106', '2019/02/27');
INSERT INTO depositor VALUES ('125-233-008', 'ACC-106', '2012/01/31');
我得到下面的结果,但这不是我想要的
+--------------------+-----------------+
| customer_name | average_balance |
+--------------------+-----------------+
| Chuck Delroy | 500000.000000 |
| Chuck Delroy | 2465.000000 |
| Chuck Delroy | 77350.000000 |
| Chuck Delroy | 480002.000000 |
| Chuck Delroy | 17000.000000 |
| Chuck Delroy | 5000.000000 |
| Dalrymple Fayval | 500000.000000 |
| Dalrymple Fayval | 2465.000000 |
| Dalrymple Fayval | 77350.000000 |
| Dalrymple Fayval | 480002.000000 |
| Dalrymple Fayval | 17000.000000 |
| Dalrymple Fayval | 5000.000000 |
| Freeman Mark | 500000.000000 |
| Freeman Mark | 2465.000000 |
| Freeman Mark | 77350.000000 |
| Freeman Mark | 480002.000000 |
| Freeman Mark | 17000.000000 |
| Freeman Mark | 5000.000000 |
| Trisman Kemar | 2465.000000 |
| Trisman Kemar | 77350.000000 |
| Trisman Kemar | 480002.000000 |
| Trisman Kemar | 17000.000000 |
| Trisman Kemar | 5000.000000 |
+--------------------+-----------------+
示例:我实际上正在寻找的是
+--------------------+-----------------+
|Delroy Chuck | 252,500.00. |
+--------------------+-----------------+
这是因为他的储蓄帐户中有500,000,而支票帐户中有5000。
答案 0 :(得分:1)
我看不到要加入客户的字段,因此我假设account
和customer
表中有一个字段customer_number
。
首先,从您的原始查询中,
SELECT account_number, balance FROM account WHERE account_number IN (SELECT account_number FROM savings_account)
可以简化为
SELECT account_number, balance
FROM account
INNER JOIN savings_account ON account.account_number = savings_account.account_number
我们要在这里包括客户以汇总平均值。
SELECT customer.customer_number, customer.customer_name, savings_account.account_number, savings_account.balance
FROM savings_account
INNER JOIN account ON account.account_number = savings_account.account_number
INNER JOIN customer ON account.customer_number = customer.customer_number
现在,对于下一步,我们将使用check_account进行合并。
SELECT customer.customer_number, customer.customer_name, checking_account.account_number, checking_account.balance
FROM checking_account
INNER JOIN account ON account.account_number = checking_account.account_number
INNER JOIN customer ON account.customer_number = customer.customer_number
然后可以将此查询与group by
聚合。
之前:
SELECT customer_name, avg(balance)
FROM
(
SELECT customer.customer_number, customer.customer_name, account.account_number, account.balance
FROM account
INNER JOIN savings_account ON account.account_number = savings_account.account_number
INNER JOIN customer ON account.customer_number = customer.customer_number
UNION ALL
SELECT customer.customer_number, customer.customer_name, account.account_number, account.balance
FROM account
INNER JOIN checking_account ON account.account_number = checking_account.account_number
INNER JOIN customer ON account.customer_number = customer.customer_number
) AS subquery2
GROUP BY customer_number;