在正确的记录行中显示总数COUNT()

时间:2019-05-07 08:51:36

标签: mysql sql

1。问题陈述

我很难在正确的记录行中显示COUNT()的总数。

如何显示COUNT()到正确的相应服务

2。背景

我想显示根据stage_id并按project_name细分员工所负责项目总数的输出。

我尝试过的/尝试过的

SELECT
    B.employee_name,
    B.project_name,
    A.total 
FROM
    (
    SELECT
        COUNT( * ) AS total,
        services.NAME AS project_name,
        services.id AS service_id,
        users.id AS member_id,
        users.NAME AS member_name 
    FROM
        users
        LEFT JOIN projects ON users.id = projects.leader_id
        LEFT JOIN services ON projects.service_id = services.id 
    WHERE
        ( projects.service_id IN ( 1, 5 ) AND projects.stage_id < 6 ) 
        OR ( projects.service_id IN ( 2, 3, 4, 7 ) AND projects.stage_id < 7 ) 
    GROUP BY
        member_name,
        service_id 
    ) AS A
    RIGHT OUTER JOIN (
    SELECT
        users.id AS user_id,
        users.NAME AS employee_name,
        services.NAME AS project_name,
        services.id AS service_id 
    FROM
        users,
    services 
    ) AS B ON A.service_id = B.user_id RIGHT OUTER JOIN ( SELECT users.id AS user_id, users.NAME AS user_name, services.NAME AS project_name, services.id AS service_id FROM users, services ) AS B ON A.service_id = B.user_id

我使用此查询在下面生成了输出

+-------+-------------------+------------------+-------------------+
|   id  |   project_name    |   employee_name  |       total       |
+-------+-------------------+------------------+-------------------+
|   1   |   Projects A      |   Employee A     |         2         |
|   2   |   Projects B      |   Employee A     |         2         |
|   3   |   Projects A      |   Employee B     |         3         |
|   4   |   Projects B      |   Employee B     |         3         |
|   5   |   Projects A      |   Employee C     |         1         |
|   6   |   Projects B      |   Employee C     |         1         |
|   7   |   Projects A      |   Employee D     |         2         |
|   8   |   Projects B      |   Employee D     |         2         |
+-------+-------------------+------------------+-------------------+

计数总数分配到错误的记录行中。

3。 SQL小提琴

这是我在SQL Fiddle上创建的输出示例。 http://www.sqlfiddle.com/#!9/08eff4/1/0

4。预期产量

我希望可以将计数总数分配给正确的记录行。

输出期望

+-------+-------------------+------------------+-------------------+
|   id  |   project_name    |   employee_name  |       total       | (COUNT (*) AS total) 
+-------+-------------------+------------------+-------------------+
|   1   |   Projects A      |   Employee A     |         2         |
|   2   |   Projects B      |   Employee A     |        NULL       |
|   3   |   Projects A      |   Employee B     |         3         |
|   4   |   Projects B      |   Employee B     |        NULL       |
|   5   |   Projects A      |   Employee C     |         1         |
|   6   |   Projects B      |   Employee C     |        NULL       |
|   7   |   Projects A      |   Employee D     |         2         |
|   8   |   Projects B      |   Employee D     |        NULL       |
+-------+-------------------+------------------+-------------------+

我已经失去了实现预期结果的想法。 因此,我想在这个论坛上寻求专家的帮助。

1 个答案:

答案 0 :(得分:0)

P / S:字段名称可能与上面的名称不同,因为我在不同的架构中编写了查询

继续尝试后,我设法找到了答案。

SELECT
    A.NAME,
    B.NAME,
    (
    SELECT
        COUNT( * ) 
    FROM
        projects
        LEFT JOIN users ON users.id = projects.leader_id 
    WHERE
        (
            projects.service_id IN ( 1, 5 ) 
            AND projects.stage_id < 6 
            AND users.id = A.id 
            AND projects.service_id = B.id 
        ) 
        OR (
            projects.service_id IN ( 2, 3, 4, 7 ) 
            AND projects.stage_id < 7 
            AND users.id = A.id 
            AND projects.service_id = B.id 
        ) 
    ) 
FROM
    users AS A
    CROSS JOIN services AS B

这是输出我预期结果的查询,如下所示。

+-------+-------------------+------------------+-------------------+
|   id  |   project_name    |   employee_name  |       total       | (COUNT (*) AS total) 
+-------+-------------------+------------------+-------------------+
|   1   |   Projects A      |   Employee A     |         2         |
|   2   |   Projects B      |   Employee A     |        NULL       |
|   3   |   Projects A      |   Employee B     |         3         |
|   4   |   Projects B      |   Employee B     |        NULL       |
|   5   |   Projects A      |   Employee C     |         1         |
|   6   |   Projects B      |   Employee C     |        NULL       |
|   7   |   Projects A      |   Employee D     |         2         |
|   8   |   Projects B      |   Employee D     |        NULL       |
+-------+-------------------+------------------+-------------------+