Union All 但只保留 T-SQL 中一个 table 的重复项
Union All but keep only duplicates from one table in T-SQL
我有两个 table 我想合并。我只需要保留两个 table 之一的重复项。我试图找到它,但找不到任何地方。希望有人能帮忙。
例如:
Table_1:
ID
Product
Amount
1
A
10
2
B
10
3
C
10
Table_2:
ID
Product
Amount
3
C
9
4
A
100
5
B
100
想要的结果:
ID
Product
Amount
1
A
10
2
B
10
3
C
9
4
A
100
5
B
100
因此请始终使用 table_2 中的重复项。在此示例中,ID 3 是重复的,因此使用 table_2 的副本,数量为 9.
如何用T-SQL实现这个?我使用了下面的代码:
Select * from Table_1 where Table_1.id != Table_2.id
Union All
Select * from Table_2
但随后我收到错误消息:
'The multi-part identifier "Table_2.ID" could not be bound.'
使用not exists
:
Select t1.*
from Table_1 t1
where not exists (select 1 from table_2 t2 where t2.id = t1.id)
Union All
Select t2.*
from Table_2 t2;
我假设你想要的是 EXISTS
:
SELECT T1.ID,
T1.Product,
T1.Amount
FROM dbo.Table1 T1
WHERE NOT EXISTS (SELECT 1
FROM dbo.Table2 T2
WHERE T1.ID = T2.ID)
UNION ALL
SELECT T2.ID,
T2.Product,
T2.Amount
FROM dbo.Table2 T2;
A FULL OUTER JOIN
,但是,也可能有效 如果 ID
在两个表中都是唯一的:
SELECT ISNULL(T2.ID,T1.ID) AS ID,
ISNULL(T2.Product,T1.Product) AS Product,
ISNULL(T2.Amount,T1.Amount) AS Amount
FROM dbo.Table1 T1
FULL OUTER JOIN dbo.Table2 T2 ON T1.ID = T2.ID;
联盟会给你结果。 Union 总是 return 唯一值。如果您使用 union all ,您将获得所有重复项。您的答案是使用 union all。
SELECT
B.ID
,B.Product
,B.Amount
FROM
(
SELECT
A.ID
,A.Product
,A.Amount
,ROW_NUMBER() over (Partition BY ID, Product order by Amount ASC) AS [row_num]
FROM
(
SELECT
tb_1.*
FROM tb_1
UNION ALL
SELECT
tb_2.*
FROM tb_2
) AS A
) AS B
WHERE B.[row_num] = 1
试试这个:
SELECT T1.*
FROM @Table1 T1
WHERE T1.ID NOT IN (SELECT ID FROM @Table2)
UNION
SELECT T2.*
FROM @Table2 T2
我有两个 table 我想合并。我只需要保留两个 table 之一的重复项。我试图找到它,但找不到任何地方。希望有人能帮忙。
例如: Table_1:
ID | Product | Amount |
---|---|---|
1 | A | 10 |
2 | B | 10 |
3 | C | 10 |
Table_2:
ID | Product | Amount |
---|---|---|
3 | C | 9 |
4 | A | 100 |
5 | B | 100 |
想要的结果:
ID | Product | Amount |
---|---|---|
1 | A | 10 |
2 | B | 10 |
3 | C | 9 |
4 | A | 100 |
5 | B | 100 |
因此请始终使用 table_2 中的重复项。在此示例中,ID 3 是重复的,因此使用 table_2 的副本,数量为 9.
如何用T-SQL实现这个?我使用了下面的代码:
Select * from Table_1 where Table_1.id != Table_2.id
Union All
Select * from Table_2
但随后我收到错误消息: 'The multi-part identifier "Table_2.ID" could not be bound.'
使用not exists
:
Select t1.*
from Table_1 t1
where not exists (select 1 from table_2 t2 where t2.id = t1.id)
Union All
Select t2.*
from Table_2 t2;
我假设你想要的是 EXISTS
:
SELECT T1.ID,
T1.Product,
T1.Amount
FROM dbo.Table1 T1
WHERE NOT EXISTS (SELECT 1
FROM dbo.Table2 T2
WHERE T1.ID = T2.ID)
UNION ALL
SELECT T2.ID,
T2.Product,
T2.Amount
FROM dbo.Table2 T2;
A FULL OUTER JOIN
,但是,也可能有效 如果 ID
在两个表中都是唯一的:
SELECT ISNULL(T2.ID,T1.ID) AS ID,
ISNULL(T2.Product,T1.Product) AS Product,
ISNULL(T2.Amount,T1.Amount) AS Amount
FROM dbo.Table1 T1
FULL OUTER JOIN dbo.Table2 T2 ON T1.ID = T2.ID;
联盟会给你结果。 Union 总是 return 唯一值。如果您使用 union all ,您将获得所有重复项。您的答案是使用 union all。
SELECT
B.ID
,B.Product
,B.Amount
FROM
(
SELECT
A.ID
,A.Product
,A.Amount
,ROW_NUMBER() over (Partition BY ID, Product order by Amount ASC) AS [row_num]
FROM
(
SELECT
tb_1.*
FROM tb_1
UNION ALL
SELECT
tb_2.*
FROM tb_2
) AS A
) AS B
WHERE B.[row_num] = 1
试试这个:
SELECT T1.*
FROM @Table1 T1
WHERE T1.ID NOT IN (SELECT ID FROM @Table2)
UNION
SELECT T2.*
FROM @Table2 T2