如何比较 SQL 中没有唯一标识符的两个表
How to compare two tables without unique identifier in SQL
我有两个结构相同但没有唯一标识符的表。如何比较这两个表
我尝试使用行号来比较它们。代码如下
WITH source AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Customer Account address) AS RowN,
Customer Account address
FROM
old
)
WITH target AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Customer Account address) AS RowN,
Customer Account address
FROM
New
)
SELECT
s.address, t.address
FROM
source s
JOIN
traget t ON s.RowN = t.RowN
WHERE
s.Customer != t.Customer
OR s.Account != t.Account
OR s.address != t.address
例外结果:
s.address t.address
---------------------
BB1 BB2
但是我得到一个错误
Incorrect syntax near the keyword 'WITH'
微软SQL服务器版本:微软SQL服务器2017
实际上,您可以使用 EXCEPT
table 运算符:
喜欢这里:
WITH
old_table(cust,accnt,addr) AS (
SELECT 'AAAA',101,'AA1'
UNION ALL SELECT 'BBBB',102,'BB1'
UNION ALL SELECT 'CCCC',102,'BB1'
)
,
new_table AS (
SELECT 'AAAA',101,'AA1'
UNION ALL SELECT 'BBBB',102,'BB1'
UNION ALL SELECT 'CCCC',102,'BB2'
)
(
SELECT * FROM old_table
EXCEPT
SELECT * FROM new_table
)
UNION ALL
(
SELECT * FROM new_table
EXCEPT
SELECT * FROM old_table
)
;
-- out cust | accnt | addr
-- out ------+-------+------
-- out CCCC | 102 | BB1
-- out CCCC | 102 | BB2
你漏掉了很多逗号 ;)
WITH source AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Customer ,Account, address) AS RowN,
Customer, Account ,address
FROM old
)
, target AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Customer, Account ,address) AS RowN,
Customer, Account, address
FROM New
)
Select s.address , t.address
from source s
join traget t on s.RowN =t.RowN
where s.Customer <> t.Customer
or s.Account <> t.Account
or s.address <> t.address
我有两个结构相同但没有唯一标识符的表。如何比较这两个表
我尝试使用行号来比较它们。代码如下
WITH source AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Customer Account address) AS RowN,
Customer Account address
FROM
old
)
WITH target AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY Customer Account address) AS RowN,
Customer Account address
FROM
New
)
SELECT
s.address, t.address
FROM
source s
JOIN
traget t ON s.RowN = t.RowN
WHERE
s.Customer != t.Customer
OR s.Account != t.Account
OR s.address != t.address
例外结果:
s.address t.address
---------------------
BB1 BB2
但是我得到一个错误
Incorrect syntax near the keyword 'WITH'
微软SQL服务器版本:微软SQL服务器2017
实际上,您可以使用 EXCEPT
table 运算符:
喜欢这里:
WITH
old_table(cust,accnt,addr) AS (
SELECT 'AAAA',101,'AA1'
UNION ALL SELECT 'BBBB',102,'BB1'
UNION ALL SELECT 'CCCC',102,'BB1'
)
,
new_table AS (
SELECT 'AAAA',101,'AA1'
UNION ALL SELECT 'BBBB',102,'BB1'
UNION ALL SELECT 'CCCC',102,'BB2'
)
(
SELECT * FROM old_table
EXCEPT
SELECT * FROM new_table
)
UNION ALL
(
SELECT * FROM new_table
EXCEPT
SELECT * FROM old_table
)
;
-- out cust | accnt | addr
-- out ------+-------+------
-- out CCCC | 102 | BB1
-- out CCCC | 102 | BB2
你漏掉了很多逗号 ;)
WITH source AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Customer ,Account, address) AS RowN,
Customer, Account ,address
FROM old
)
, target AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Customer, Account ,address) AS RowN,
Customer, Account, address
FROM New
)
Select s.address , t.address
from source s
join traget t on s.RowN =t.RowN
where s.Customer <> t.Customer
or s.Account <> t.Account
or s.address <> t.address