所以我正在验证源表和目标表之间的计数,如果计数不同,它可以是正数或负数,但是当是负数时它会显示 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)
问题在这里:
(从 cte 中选择 total_count 其中 [object] = 'target')
“cte” cte 中没有 object = 'target'。
由于select返回null,计算也必须返回null。
试试这个:(将 cte 更改为 cte1)
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 cte1 where [object] = 'target')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
289 次 |
最近记录: |