在比较表时插入 1 或 0 where EXCEPT returns a value

Insert 1 or 0 where EXCEPT returns a value when comparing tables

使用 SQL Server 2014,我如何 return 1 或 0 的 table,然后计算 tables 中匹配的 1对于多个字段名称,按 [贷款标识符] 分组。我目前有一个名为 dbo.Tape 的 table,它是原始的 table,然后是用户正在更新的 dbo.[Tape Capture]。 [贷款标识符] 在两个 table 中是相同的。

谢谢,如果你能帮忙。

SELECT       
   [Loan Identifier], [Completion date], [Tenure], [Underwriter name] 
FROM 
   dbo.Tape

EXCEPT

SELECT      
    [Loan Identifier], [Completion date], [Tenure], [Underwriter name]  
FROM 
    dbo.[Tape Capture]

如果 dbo.Tape 中的 [Completion date]dbo.[Tape Capture] 中的 [Completion date] 匹配,那么我想 return 1,否则为零。然后我想在每一列中总结所有这些值。例如,

dbo.[磁带捕获]:

[Loan Identifier], [Completion date], [Tenure], [Underwriter name] 
1                    01/01/2016       Freehold     James Mac
2                    01/01/2016       Leasehold    James Mac
3                    02/01/2016       Freehold     James Mac
4                    01/01/2016       Leasehold    James Mac
5                    03/01/2016       Freehold     James Mac

dbo.Tape:

[Loan Identifier], [Completion date], [Tenure], [Underwriter name] 
1                    01/01/2016       Freehold     James Mac
2                    01/01/2016       Freehold     James Mac
3                    01/01/2016       Freehold     James Mac
4                    01/01/2016       Freehold     James Mac
5                    01/01/2016       Freehold     James Mac

那么我希望结果table保持:

[Loan Identifier], [Completion date], [Tenure], [Underwriter name] 
1                    1                  1          1
2                    1                  0          1
3                    0                  1          1
4                    1                  0          1
5                    0                  1          1

从这里开始,我想总结每一列中的值。

SELECT T.[Loan Identifier], 
       CASE 
         WHEN TC.[Completion date] = T.[Completion date] THEN 1 
         ELSE 0 
       END AS [Completion date], 
       CASE 
         WHEN TC.[Tenure] = T.[Tenure] THEN 1 
         ELSE 0 
       END AS [Tenure], 
       CASE 
         WHEN TC.[Underwriter name] = T.[Underwriter name] THEN 1 
         ELSE 0 
       END AS [Underwriter name] 
FROM   [dbo].[Tape] AS T 
       LEFT JOIN [dbo].[Tape Capture] AS TC 
              ON T.[Loan Identifier] = TC.[Loan Identifier]