连接两个具有不同数据的相同表结构

发布于 2024-12-11 16:26:03 字数 1318 浏览 0 评论 0原文

编辑: 尝试 COALESCE 方法后,我现在看到一个问题,即每个瓦数类别的数据重复使用相同的数据。第 2 列是瓦数。 在此处输入图像描述

我创建了两个临时表,它们都具有完全相同的表结构。在这些表中,多个列可能具有相同的值,然后一些值列可能具有不同的数字。其中一些在一列中为 NULL,而在另一列中则不为 NULL。我想将所有值放在一起,并且在具有相同站点和工厂的行上,我希望将这些值连接起来。

下面是两个表的示例以及我期望的结果

表 1:

SITE      PLANT          VALUE_1           VALUE 2
S1        P1             54                66
S1        P2             43                43

表 2:

SITE      PLANT         VALUE_1           VALUE_2
S1        P1            33                43
S2        P1            34                22

结果:

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

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

I've created two temp tables, both with the exact same table structure. In these tables, there are multiple columns that could have the same values, and then a few value columns that will have different numbers. Some of these will be NULL in one column, and not null in another. I want to get all the values together, and on rows with the same site and plant I'd like the values joined.

Here is an example of what the two tables could look like and the result I'd expect

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

My original thoughts would be a full join. However, this doesn't work because in your select statement you must specify where to grab the columns from, like site and plant; but to select both t1.site and t2.site would generate two columns. The closest thing I got was the query below, however anytime there is a result in S2 that has a site and plant not in S1, you receive null values for S1 and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-12-18 16:26:03

完成这个查询需要两个技巧。第一个是 FULL JOIN。完全联接允许您组合两个表,并在不匹配联接条件时在任一表中插入空值。第二个是 COALESCE,它允许您从提供结果中该行记录的表中获取工厂和站点。

SELECT Coalesce(t1.Site,t2.Site) As Site, COALESCE(t1.Plant, t2.Plant) As Plant,
    t1.Value_1 As t1_Value_1, t1.Value_2 As t1_Value_2,
    t2.Value_1 As t2_Value_1, t2.Value_2 As t2_Value_2
FROM Table1 t1
FULL JOIN Table2 t2 ON t1.Site = t2.Site AND t1.Plant = t2.Plant

Two tricks are needed to complete this query. The first is a FULL JOIN. A full join will allow you to combine both tables, and insert nulls in either table when you don't match the join condition. The 2nd is COALESCE, which will allow you take the plant and site from whichever of the tables provides a record for this row in the results.

SELECT Coalesce(t1.Site,t2.Site) As Site, COALESCE(t1.Plant, t2.Plant) As Plant,
    t1.Value_1 As t1_Value_1, t1.Value_2 As t1_Value_2,
    t2.Value_1 As t2_Value_1, t2.Value_2 As t2_Value_2
FROM Table1 t1
FULL JOIN Table2 t2 ON t1.Site = t2.Site AND t1.Plant = t2.Plant
荆棘i 2024-12-18 16:26:03

您可以使用合并。它将返回参数中的第一个非空值。

SELECT coalesce(t1.Site, t2.Site) as Site, coalesce(t1.Plant, t2.Plant) as Plant,

You can use coalesce. It will return the first non null value from the parameters.

SELECT coalesce(t1.Site, t2.Site) as Site, coalesce(t1.Plant, t2.Plant) as Plant,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文