简体   繁体   English

SQL计算一个表相对于另一表的行

[英]SQL count rows of one table in relation to another table

I have 3 different tables : 我有3个不同的表:

Client 客户

+----+-----------+----------+
| id | firstName | lastName |
+----+-----------+----------+
|  1 | John      | Doe      |
|  2 | Jane      | Doe      |
+----+-----------+----------+

Loan 贷款

+----+--------+-----------+----------------+
| id | amount | client_id | institution_id |
+----+--------+-----------+----------------+
|  1 |    200 |         2 |              3 |
|  2 |    400 |         1 |              1 |
+----+--------+-----------+----------------+

Institution 机构

+----+---------------+
| id |     name      |
+----+---------------+
|  1 | Institution A |
|  2 | Institution B |
|  3 | Institution C |
+----+---------------+

I am looking to create a list of the number of loans a client has with each institution (for every row in the institution table). 我正在寻找一个清单,列出每个机构向客户提供的贷款数量(机构表中的每一行)。 Including when a client has 0 loans with an institution. 包括客户在某机构获得0笔贷款时。

Something that looks like : 看起来像:

+-----------+-----------+----------+--------------------------+-----------+
| client_id | firstName | lastName | financialInstitutionName | loanCount |
+-----------+-----------+----------+--------------------------+-----------+
|         1 | John      | Doe      | Institution A            |         1 |
|         1 | John      | Doe      | Institution B            |         0 |
|         1 | John      | Doe      | Institution C            |         0 |
|         2 | Jane      | Doe      | Institution A            |         0 |
|         2 | Jane      | Doe      | Institution B            |         0 |
|         2 | Jane      | Doe      | Institution C            |         1 |
+-----------+-----------+----------+--------------------------+-----------+

I have tried all manners of joins, subqueries and where clauses but without success. 我尝试了各种方式的连接,子查询和where子句,但均未成功。 The concept that I do not grasp is how to get a row per institution, per client (total count institution x client). 我不理解的概念是如何在每个机构,每个客户(总机构x客户)中获得一行。 I would love if that query was possible without subqueries or union joins. 我很想如果没有子查询或联合联接就可以进行该查询。

Thank you for your time! 感谢您的时间!

SELECT 
    loan.client_id, 
    client.firstName, 
    client.lastName, 
    institution.name as financialInstitutionName, 
    COUNT(loan.id) as loanCount 
FROM client 
INNER JOIN loan ON client.id = loan.client_id 
INNER JOIN institution ON loan.institution_id = institution.id 
GROUP BY client.id;

First subquery in the FROM setups that data so each client has a record for each of the institutions. FROM中的第一个子查询会设置该数据,以便每个客户都有每个机构的记录。 This is then joined to a subquery that counts the number of loans. 然后将其连接到一个子查询,该子查询计算贷款的数量。

SELECT 
    d.client_id,
    d.firstName,
    d.lastName,
    d.name AS financialInstitutionName,
    CASE WHEN l IS NULL
        THEN 0
        ELSE l.loanCount
    END AS loanCount

FROM
(
    SELECT
        Client.id AS client_id,
        Client.firstName,
        Client.lastName,
        Institution.id AS institution_id,
        Institution.name
    FROM Client, Institution
) AS d

LEFT JOIN (
    SELECT client_id, institution_id, COUNT(id) AS loanCount 
    FROM Loan
    GROUP BY client_id, institution_id
) AS l ON d.client_id = l.client_id AND d.institution_id = l.institution_id

Edit: Includes a record for each institution 编辑:包括每个机构的记录

Edit: Spelling 编辑:拼写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM