简体   繁体   English

将表合并为NULL

[英]Merge tables where NULL

I have two tables like this 我有两张这样的桌子

Table 1 表格1

+-----------+----------+-------+-------------------+
| AGENCY_ID | LOCAL_ID | CLASS |      LRS_ID       |
+-----------+----------+-------+-------------------+
|       651 | 1        | NULL  | 10200000690000001 |
|       651 | 2        | NULL  | 10200000690205B01 |
|       651 | 3        | NULL  | 10200000690293C01 |
|       651 | 4        | NULL  | 10200000690293D01 |
+-----------+----------+-------+-------------------+

Table 2 表2

+-----------+----------+-------+-------------------+
| AGENCY_ID | LOCAL_ID | CLASS |      LRS_ID       |
+-----------+----------+-------+-------------------+
|       651 | NULL     | 1     | 10200000690000001 |
|       651 | NULL     | NULL  | 10200000690000091 |
|       651 | NULL     | 7     | 10200000690205B01 |
|       651 | NULL     | 1     | 10200000690293C01 |
+-----------+----------+-------+-------------------+

And I want the result to be 我希望结果如此

+-----------+----------+-------+-------------------+
| AGENCY_ID | LOCAL_ID | CLASS |      LRS_ID       |
+-----------+----------+-------+-------------------+
|       651 | 1        | 1     | 10200000690000001 |
|       651 | 2        | 7     | 10200000690205B01 |
|       651 | 3        | 1     | 10200000690293C01 |
|       651 | 4        | NULL  | 10200000690293D01 |
+-----------+----------+-------+-------------------+

Taking table 2 and merging the non-NULL values to table 1. Here I only specify the CLASS field but there are 50+ fields that are always NULL in Table 1 and may or may not be NULL in table 2. So just specifying manually which fields I want is the problem, I want it to automatically replace it if its NULL and table 2 has it. 取表2并将非NULL值合并到表1.这里我只指定CLASS字段,但表1中有50多个字段始终为NULL,表2中可能有也可能不为NULL。所以只需手动指定我想要的字段是问题,如果它的NULL和表2有它,我希望它自动替换它。

Key things to note is that LRS_ID is the JOIN key. 需要注意的关键是LRS_ID是JOIN键。 LRS_ID that exist in table 2 and not table 1 don't exist in the output. 表2中存在的LRS_ID而不是表1中的LRS_ID在输出中不存在。 LRS_ID that exists in table 1 but not table 2 remain but CLASS remains NULL. 存在于表1但不存在于表2中的LRS_ID仍然存在,但CLASS保持为NULL。

You could use left join to get your expected output like this: 您可以使用left join来获得预期的输出,如下所示:

select
    t1.AGENCY_ID,
    ISNULL(t1.LOCAL_ID,t2.LOCAL_ID)LOCAL_ID,
    ISNULL(t1.CLASS,t2.CLASS)CLASS,
    t1.LRS_ID
from table1 t1 
left join table2 t2 on
    t1.LRS_ID = t2.LRS_ID

SQL Fiddle Demo SQL小提琴演示

Just join the tables and find the first value you want. 只需加入表格即可找到您想要的第一个值。 Select: 选择:

select
    t1.AGENCY_ID,
    ISNULL(t1.LOCAL_ID,t2.LOCAL_ID)LOCAL_ID,
    ISNULL(t1.CLASS,t2.CLASS)CLASS,
    t1.LRS_ID
from table1 t1 
inner join table2 t2 on
    t1.LRS_ID = t2.LRS_ID

Reverse ISNULL to give priority to t2. 反向ISNULL优先于t2。

To update: 更新:

update t
    LOCAL_ID = ISNULL(t.LOCAL_ID,t2.LOCAL_ID),
    CLASS = ISNULL(t.CLASS,t2.CLASS)
from table1 t
inner join table2 t2 on
    t1.LRS_ID = t2.LRS_ID

First you get a distinct list of all the LRS_ID's from both tables, as some may exist in one table but not the other. 首先,您将获得两个表中所有LRS_ID的不同列表,因为某些表可能存在于一个表中而不存在于另一个表中。 Then, outer join to both tables based on the LRS_ID. 然后,基于LRS_ID对两个表进行外连接。 Finally, use COALESCE to get the first non-null value from either table for each field. 最后,使用COALESCE从每个字段的任一表中获取第一个非空值。

SELECT
    DerivedAllLRS_IDs.LRS_ID,
    COALESCE([Table 1].AGENCY_ID, [Table 2].AGENCY_ID) AS AGENCY_ID,
    COALESCE([Table 1].LOCAL_ID, [Table 2].LOCAL_ID) AS LOCAL_ID,
    COALESCE([Table 1].CLASS, [Table 2].CLASS) AS CLASS
FROM
    (
    SELECT DISTINCT 
        LRS_ID
    FROM
        (
        (SELECT LRS_ID FROM [Table 1])
        UNION ALL
        (SELECT LRS_ID FROM [Table 2])
        )DerivedUnion
    ) DerivedAllLRS_IDs
    LEFT OUTER JOIN [Table 1] ON DerivedAllLRS_IDs.LRS_ID = [Table 1].LRS_ID
    LEFT OUTER JOIN [Table 2] ON DerivedAllLRS_IDs.LRS_ID = [Table 2].LRS_ID

Note that if you are looking to modify the underlying data, it will require two passes. 请注意,如果您要修改基础数据,则需要两次通过。 First, an UPDATE statement on existing records in [Table 1] to plug the missing info that exists in [Table 2] , then a set of INSERT statements to get data for rows where the LRS_ID only exists in [Table 2] into [Table 1] . 首先, UPDATE在现有记录的语句[Table 1]堵塞存在于缺少信息[Table 2]然后是一组INSERT语句以获取行的数据,其中LRS_ID只存在[Table 2][Table 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM