比较和合并SQL Server 2005中的两个表

时间:2014-02-05 02:26:33

标签: sql sql-server sql-server-2005

我有两个脚本生成以下两个表(t1和t2)。 有没有办法合并t1和t2来制作t3? 每个表的前5行在第一列(Number)中具有相同的值。 但是在t1中填充了PreOpOrg和PreOpModality列,但是在t2中它们是NULL,反之亦然,对于PostOpOrg和PostOpModality列。 因此,合并将使用每个表(t1和t2)中的非NULL值填充列。对于仅出现在一个表中的t1和t2中的行,它们应该出现在t3中,就像正在尝试正常的UNION一样。 不幸的是,当我在SQL2005中工作时,脚本需要与SQL2000兼容。 非常感谢。

    --t1

    Number          DateOfBirth             PreOpOrg    PreOpModality   PostOpOrg   PostOpModality  DiagDate
    8643576344      01/04/1935  00:00:00    RBA11       2               NULL        NULL            04/09/2012 00:00
    2345680016      01/01/1960  00:00:00    RBA11       4               NULL        NULL            11/11/2011 00:00
    6955934252      20/01/1980  00:00:00    RBA11       4               NULL        NULL            22/08/2012 00:00
    2234582369      28/12/1940  00:00:00    RBA11       5               NULL        NULL            02/11/2011 00:00
    9477684368      01/02/1959  00:00:00    RBA11       5               NULL        NULL            02/01/2014 00:00

    --t2

    Number          DateOfBirth             PreOpOrg    PreOpModality   PostOpOrg   PostOpModality  DiagDate
    8643576344      01/04/1935  00:00:00    NULL        NULL            RBA11       2               04/09/2012 00:00
    2345680016      01/01/1960  00:00:00    NULL        NULL            RBA11       2               11/11/2011 00:00
    6955934252      20/01/1980  00:00:00    NULL        NULL            RN325       5               22/08/2012 00:00
    2234582369      28/12/1940  00:00:00    NULL        NULL            RBA11       5               02/11/2011 00:00
    9477684368      01/02/1959  00:00:00    NULL        NULL            RBA11       5               02/01/2014 00:00
    0000001564      01/01/1970  00:00:00    NULL        NULL            RBA11       2               08/08/2012 00:00
    4225127842      11/07/1948  00:00:00    NULL        NULL            RBA11       2               10/09/2011 00:00
    0909093842      01/07/1957  00:00:00    NULL        NULL            RN325       5               26/08/2009 00:00
    8528528855      05/12/1969  00:00:00    NULL        NULL            RBA11       5               25/10/2010 00:00
    1000001367      05/05/1950  00:00:00    NULL        NULL            RBA11       5               08/04/2011 00:00
    2234573513      30/11/1946  00:00:00    NULL        NULL            RN325       5               20/04/2011 00:00
    1000001566      18/03/1951  00:00:00    NULL        NULL            RBA11       5               20/03/2012 00:00
    0000001600      01/01/1970  00:00:00    NULL        NULL            RBA11       5               28/03/2013 00:00
    1000001621      12/12/1950  00:00:00    NULL        NULL            RBA11       5               06/12/2013 00:00

    --t3    

    Number          DateOfBirth             PreOpOrg    PreOpModality   PostOpOrg   PostOpModality  DiagDate
    8643576344      01/04/1935  00:00:00    RBA11       2               RBA11       2               04/09/2012 00:00
    2345680016      01/01/1960  00:00:00    RBA11       4               RBA11       2               11/11/2011 00:00
    6955934252      20/01/1980  00:00:00    RBA11       4               RN325       5               22/08/2012 00:00
    2234582369      28/12/1940  00:00:00    RBA11       5               RBA11       5               02/11/2011 00:00
    9477684368      01/02/1959  00:00:00    RBA11       5               RBA11       5               02/01/2014 00:00
    0000001564      01/01/1970  00:00:00    NULL        NULL            RBA11       2               08/08/2012 00:00
    4225127842      11/07/1948  00:00:00    NULL        NULL            RBA11       2               10/09/2011 00:00
    0909093842      01/07/1957  00:00:00    NULL        NULL            RN325       5               26/08/2009 00:00
    8528528855      05/12/1969  00:00:00    NULL        NULL            RBA11       5               25/10/2010 00:00
    1000001367      05/05/1950  00:00:00    NULL        NULL            RBA11       5               08/04/2011 00:00
    2234573513      30/11/1946  00:00:00    NULL        NULL            RN325       5               20/04/2011 00:00
    1000001566      18/03/1951  00:00:00    NULL        NULL            RBA11       5               20/03/2012 00:00
    0000001600      01/01/1970  00:00:00    NULL        NULL            RBA11       5               28/03/2013 00:00
    1000001621      12/12/1950  00:00:00    NULL        NULL            RBA11       5               06/12/2013 00:00

2 个答案:

答案 0 :(得分:2)

考虑到您的数据和要求在SQL 2000上运行,我将为您提供以下解决方案 如果您遇到任何问题,请告诉我。为方便起见,我只添加了样本中的少量记录。如果您使用的是SQL Server 2008或更高版本,则可以使用COALESCE函数,如here所述。

create table t1
(
 Number  VARCHAR(10),
 DateOfBirth DATETIME,
 PreOpOrg VARCHAR(10),
 PreOpModality INT,   
 PostOpOrg VARCHAR(10),  
 PostOpModality INT,  
 DiagDate DATETIME
)

create table t2
(
 Number  VARCHAR(10),
 DateOfBirth DATETIME,
 PreOpOrg VARCHAR(10),
 PreOpModality INT,   
 PostOpOrg VARCHAR(10),  
 PostOpModality INT,  
 DiagDate DATETIME
)

INSERT INTO T1 values (8643576344,'01/04/1935  00:00:00','RBA11',2,NULL,NULL,'04/09/2012 00:00'),
(2345680016,'01/01/1960  00:00:00','RBA11',4,NULL,NULL,'11/11/2011 00:00')

INSERT INTO T2 values ('8643576344','01/04/1935  00:00:00',NULL,NULL,'RBA11',2,'04/09/2012 00:00'),
('0000001564','01/01/1970  00:00:00',NULL,NULL,'RBA11',2,'08/08/2012 00:00'),
('2345680016','01/01/1960  00:00:00',NULL,NULL,'RBA11',2,'11/11/2011 00:00' )


SELECT * FROM T1
SELECT * FROM T2


SELECT CASE WHEN T1.Number IS NOT NULL THEN T1.Number ELSE T2.NUMBER END AS Number,
CASE WHEN T1.DateOfBirth IS NOT NULL THEN T1.DateOfBirth ELSE T2.DateOfBirth END AS DateOfBirth,
CASE WHEN T1.PreOpOrg IS NOT NULL THEN T1.PreOpOrg ELSE T2.PreOpOrg END AS PreOpOrg,
CASE WHEN T1.PreOpModality IS NOT NULL THEN T1.PreOpModality ELSE T2.PreOpModality END AS PreOpModality,
CASE WHEN T1.PostOpOrg IS NOT NULL THEN T1.PostOpOrg ELSE T2.PostOpOrg END AS PostOpOrg,
CASE WHEN T1.PostOpModality IS NOT NULL THEN T1.PostOpModality ELSE T2.PostOpModality END AS PostOpModality,
CASE WHEN T1.DiagDate IS NOT NULL THEN T1.DiagDate ELSE T2.DiagDate END AS DiagDate
FROM T1
RIGHT JOIN  T2 ON T1.Number = T2.Number

答案 1 :(得分:2)

另一种解决方案可以是

    SELECT 
        ISNULL(T1.Number, T2.NUMBER) AS Number
        ,ISNULL(T1.DateOfBirth, T2.DateOfBirth) AS DateOfBirth
        ,ISNULL(T1.PreOpOrg, T2.PreOpOrg) AS PreOpOrg
        ,ISNULL(T1.PreOpModality, T2.PreOpModality) AS PreOpModality
        ,ISNULL(T1.PostOpOrg, T2.PostOpOrg) AS PostOpOrg
        ,ISNULL(T1.PostOpModality, T2.PostOpModality) AS PostOpModality
        ,ISNULL(T1.DiagDate, T2.DiagDate) AS DiagDate
    FROM T1
    RIGHT JOIN  T2 
        ON T1.Number = T2.Number