需要基于其他 table 的交易 table 计数,包括没有匹配项的零

Need count of transactional table based on other tables including zeros where there are no matches

我有四个 table,其中三个非常静态:haul_types、dumpster_type_team(dumpster_type_team 与 many-to-many 之间的关系=56=] 和团队)和用户。第四个 table,hauls,有交易数据。

haul_types:

id
name

dumpster_type_team:

id
dumpster_type_id
team_id

users:

id
first_name
last_name
is_driver
team_id

hauls:

haul_type_id
haul_status_id
set_dumpster_type_id
completed_driver_id
team_id

我想要一个包含 dumpster_types、haul_types 和 driver 的组合的查询(用户)以及他们参与的拖运计数。在一些在某些情况下,计数应该为零,因为某些 driver 还没有完成每个 haul_type / 垃圾箱类型组合的运输。

到目前为止,这是我的查询,它的行为似乎是内部联接,因为记录正在过滤以仅显示匹配项:

SELECT
    c.haul_type_id,
    c.dumpster_type_id,
    c.driver_id,
    count(h.id) AS haul_count
FROM
    hauls h
    RIGHT JOIN ( SELECT DISTINCT
            ht.id AS haul_type_id,
            dtt.dumpster_type_id AS dumpster_type_id,
            dtt.team_id AS team_id,
            u.id AS driver_id
        FROM
            haul_types ht
            CROSS JOIN dumpster_type_team dtt
            CROSS JOIN users u
        WHERE
            u.team_id = dtt.team_id
            AND u.is_driver = TRUE) c ON c.haul_type_id = h.haul_type_id
    AND c.dumpster_type_id = h.set_dumpster_type_id
    AND c.driver_id = h.completed_driver_id
    AND c.team_id = h.team_id
WHERE
    h.team_id = 9
    AND h.haul_status_id = 3
    AND h.completed_driver_id IS NOT NULL
GROUP BY
    c.haul_type_id, c.dumpster_type_id, c.driver_id

当我运行隔离子查询时:

SELECT DISTINCT
            ht.id AS haul_type_id,
            dtt.dumpster_type_id AS dumpster_type_id,
            dtt.team_id AS team_id,
            u.id AS driver_id
        FROM
            haul_types ht
            CROSS JOIN dumpster_type_team dtt
            CROSS JOIN users u
        WHERE
            u.team_id = dtt.team_id
            AND u.is_driver = TRUE

我得到了我想要的结果:haul_type、dumpster_type、driver_id 和 team_id 的每个排列对应一行。但是,当我 运行 整个查询时,尽管有正确的连接,我还是得到过滤结果。

我想要的是: 如果我有 4 haul_types:送货、换货、直播、取货

和 2 dumpster_types:10 岁,15 岁

和 2 drivers: 1, 2

我想要 haul_type、dumpster_type 和 driver 组合的运输计数。如果没有匹配该行的运输,则显示 0:

感谢任何帮助。谢谢

问题的描述和查询好像关系不大。我不知道什么是“枢轴 table”。

I would like a query that has a combination of dumpster_types, haul_types, and drivers (users) and a count of the hauls they were involved in.

这听起来像是 cross join 生成行,然后 left join/group by 计算结果:

select d.dumpster_id, ht.haul_type_id, d.driver_id, count(h.driver_id)
from dumpster_types d cross join
     haul_types ht cross join
     drivers d left join
     hauls h
     on h.dumpster_id = d.dumpster_id and
        h.haul_type_id = ht.haul_type_id and
        h.driver_id = d.driver_id
group by d.dumpster_id, ht.haul_type_id, d.driver_id;

运行 @GordonLinoff 提供的查询暴露了我面临的问题 - 在顶级查询上应用 where 子句时,结果被过滤为仅匹配。我将 where 子句移动到各个子查询,现在我得到了所有预期结果。

不确定这是否是最有效的编写方式,但它会产生正确的结果:

SELECT
    d.dumpster_type_id,
    ht.id AS haul_type_id,
    u.id AS driver_id,
    count(h.id) AS haul_count
FROM (
    SELECT
        dumpster_type_id,
        team_id
    FROM
        dumpster_type_team
    WHERE
        team_id = 9) d
    CROSS JOIN haul_types ht
    CROSS JOIN (
        SELECT
            users.id
        FROM
            users
        WHERE
            users.is_driver = TRUE
            AND users.team_id = 9) u
    LEFT JOIN (
        SELECT
            id, set_dumpster_type_id, haul_type_id, completed_driver_id, team_id
        FROM
            hauls
        WHERE
            haul_status_id = 3
            AND team_id = 9) h ON h.set_dumpster_type_id = d.dumpster_type_id
    AND h.haul_type_id = ht.id
    AND h.completed_driver_id = u.id
    AND h.team_id = d.team_id
GROUP BY
    d.dumpster_type_id,
    ht.id,
    u.id