Edit:
After attempting the COALESCE method, I'm now seeing an issue where the data is repeating itself with the same data for each wattage category. Column 2 is wattage.
![enter image description here](https://i.stack.imgur.com/6Az7I.png)
我创建了两个临时表,它们都具有完全相同的表结构。在这些表中,多个列可能具有相同的值,然后一些值列可能具有不同的数字。其中一些在一列中为 NULL,而在另一列中则不为 NULL。我想将所有值放在一起,并且在具有相同站点和工厂的行上,我希望将这些值连接起来。
这是两个表的外观以及我期望的结果的示例
TABLE1:
SITE PLANT VALUE_1 VALUE 2
S1 P1 54 66
S1 P2 43 43
TABLE 2:
SITE PLANT VALUE_1 VALUE_2
S1 P1 33 43
S2 P1 34 22
RESULT:
SITE PLANT t1_VALUE_1 t1_VALUE_2 t2_VALUE_1 t2_VALUE2
S1 P1 54 66 33 43
S1 P2 43 43 NULL NULL
S2 P1 NULL NULL 34 22
我最初的想法是完全加入。但是,这不起作用,因为在 select 语句中,您必须指定从何处获取列,例如站点和工厂;但同时选择 t1.site 和 t2.site 将生成两列。我得到的最接近的是下面的查询,但是只要 S2 中的结果具有不在 S1 中的站点和工厂,您就会收到 S1 和 S2 的空值。
SELECT t1.Site, t1.Plant, t1.Value_1, t1.Value_2, t2.Value_1, t2.Value_2
FROM table1 t1
FULL JOIN table2 t2
ON t1.site = t2.site
AND t1.plant = t2.plant