所以我正在验证源表和目标表之间的计数,如果计数不同,它可以是正数或负数,但是当是负数时它会显示 NULL。我曾经知道这一点,但在我看来,我的记忆力很差。我的代码如下。
with cte as (
select 'source' [object],count(contract_id) as total_count
FROM [account].[dbo].[account] act
left join [account].[dbo].[contract] cont
on act.account_id = cont.account_id)
,cte1 as(
select 'target' [object], count(contract_id) as total_count
from [account].[dbo].[action]
union
select * from cte)
select * from cte1
union
select 'diff' [object],
(select total_count from cte where [object] = 'source')
-
(select total_count from cte where [object] = 'target')
Run Code Online (Sandbox Code Playgroud)
结果如下 object total_count target 28402 source 28401 diff NULL -- 这里应该是负 1 (-1)
sql-server ×1