简体   繁体   English

如何合并两个表并覆盖先前存在的行

[英]How to Union Two Tables and Overwrite Preexisting Rows

I want to combine two tables into one, but if in table 1 the account code "Acc1" is the same as in table 2 the account code "Acc1" then I want to take the amount in table 1 as the result我想将两个表合二为一,但如果表 1 中的帐户代码“Acc1”与表 2 中的帐户代码“Acc1”相同,那么我想将表 1 中的金额作为结果

Table 1表格1
Account Code AccountName Project Type Desc Period Amount科目代码 AccountName 项目类型 Desc Period Amount

Bcc1 AccountBone AA Good PC2000 11/30/2022 700 Bcc1 AccountBone AA 良好 PC2000 11/30/2022 700

Acc1 AccountOne AA Good PC2000 12/1/2022 300 Acc1 AccountOne AA 良好 PC2000 12/1/2022 300

Table 2表 2
Account Code AccountName Project Type Desc Period Amount科目代码 AccountName 项目类型 Desc Period Amount

Acc1 AccountOne AA Good PC2000 12/1/2022 220 Acc1 AccountOne AA 良好 PC2000 12/1/2022 220

Acc2 AccountOne AA Good PC2000 12/2/2022 432 Acc2 AccountOne AA 良好 PC2000 12/2/2022 432

Result结果
Account Code AccountName Project Type Desc Period Amount科目代码 AccountName 项目类型 Desc Period Amount

Bcc1 AccountBone AA Good PC2000 11/30/2022 700 Bcc1 AccountBone AA 良好 PC2000 11/30/2022 700

Acc1 AccountOne AA Good PC2000 12/1/2022 300 Acc1 AccountOne AA 良好 PC2000 12/1/2022 300

Acc2 AccountOne AA Good PC2000 12/2/2022 432 Acc2 AccountOne AA 良好 PC2000 12/2/2022 432

I was expecting a query for this case我期待对这种情况的查询

One way would be a full outer join and an unpivoting technique to select the columns from T1 if there was a match or T2 otherwise ( DB Fiddle )一种方法是完全外部连接和 select 的反透视技术,如果匹配则来自T1T2否则( DB Fiddle

SELECT CA.*
FROM   Table1 t1
       FULL JOIN Table2 t2
              ON t1.AccountCode = t2.AccountCode
       CROSS APPLY (SELECT t1.AccountCode,
                           t1.AccountName /*and other columns*/
                    WHERE  t1.AccountCode IS NOT NULL
                    UNION ALL
                    SELECT t2.AccountCode,
                           t2.AccountName /*and other columns*/
                    WHERE  t1.AccountCode IS NULL) CA 

The question is a bit confusing.这个问题有点混乱。 However, based on your results wouldn't this just work:但是,根据您的结果,这不会奏效:

DROP TABLE IF EXISTS #TempTable1;
DROP TABLE IF EXISTS #TempTable2;

CREATE TABLE #TempTable1
(
    AccountCode VARCHAR(100),
    AccountName VARCHAR(100)
);

CREATE TABLE #TempTable2
(
    AccountCode VARCHAR(100),
    AccountName VARCHAR(100)
);

GO

INSERT INTO #TempTable1(AccountCode, AccountName)
VALUES('Bcc1', 'AccountBone'),
('Acc1', 'AccountOne');

INSERT INTO #TempTable2(AccountCode, AccountName)
VALUES('Acc1', 'AccountOne'),
('Acc2', 'AccountOne');

SELECT *
FROM #TempTable1
UNION
SELECT *
FROM #TempTable2;

You could select all from Table1 and from Table2 select all that do not exist in Table1:您可以 select 全部来自 Table1 和 Table2 select 全部不存在于 Table1 中:

select * 
from Table1
union
select * 
from Table2
where AccountCode not in (
  select AccountCode
  from Table1
)

See a dbfiddle查看dbfiddle

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

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