两列的联接表仅将其联接在一个表上

时间:2020-08-17 12:30:43

标签: sql postgresql

如何正确地将两列的表联接在一起。我的问题是结果不正确,因为它只连接在一个列上。

这个问题开始于另一个问题:SQL query returns product of results instead of sum。我正在创建一个新问题,因为我正在尝试解决另一个问题。

我将一个物料表连接到一个包含多个供应和处置移动的表上。每个移动都引用一个物料ID。我想加入每个乐章的内容。

我的查询:

SELECT supply_material_refer, disposal_material_refer, material_id, material_name
FROM "construction_sites"
         JOIN projects ON construction_sites.project_refer = projects.project_id
         JOIN addresses ON construction_sites.address_refer = addresses.address_id
         cross join lateral ( select *
                              from (select row_number() over () as rn, *
                                    from supplies
                                    where supplies.supply_project_refer = projects.project_id) as supplies
                                       full join (select row_number() over () as rn, *
                                                  from disposals
                                                  where disposals.disposal_project_refer = projects.project_id
                              ) as disposals
                                                 on (supplies.rn = disposals.rn)
    ) as combined
         LEFT JOIN materials material ON combined.disposal_material_refer = material.material_id
    OR combined.supply_material_refer = material.material_id
WHERE (projects.project_name = 'Project 15')
ORDER BY construction_site_id asc;

查询结果:

+-----------------------+-------------------------+-------------+---------------+
| supply_material_refer  | disposal_material_refer | material_id | material_name |
+-----------------------+-------------------------+-------------+---------------+
|                    1  | 1                       |          1  | Materialtest  |
|                    2  | 1                       |          1  | Materialtest  |
|                    2  | 1                       |          2  | Dirt          |
|                    1  | 1                       |          1  | Materialtest  |
|                    2  | 1                       |          1  | Materialtest  |
|                    2  | 1                       |          2  | Dirt          |
|                    1  | (null)                  |          1  | Materialtest  |
|                    4  | (null)                  |          4  | Stones        |
+-----------------------+-------------------------+-------------+---------------+

我遇到问题的示例行

+------------------------+-------------------------+-------------+---------------+
| supply_material_refer  | disposal_material_refer | material_id | material_name |
+------------------------+-------------------------+-------------+---------------+
|                     2  |                      1  |          1  | Materialtest  |
+------------------------+-------------------------+-------------+---------------+

首选输出如下:

+------------------------+----------------------+-------------------------+------------------------+
| supply_material_refer  | supply_material_name | disposal_material_refer | disposal_material_name |
+------------------------+----------------------+-------------------------+------------------------+
|                     2  | Dirt                 |                      1  | Materialtest           |
+------------------------+----------------------+-------------------------+------------------------+

我已经创建了一个带有伪数据的sqlfiddle:http://www.sqlfiddle.com/#!17/863d78/2

据我了解,解决方案将是为物料名称设置一个处置物料列和供应物料列。我不知道如何实现这个目标...

感谢您的帮助!

0 个答案:

没有答案