使用SQL Server 2014,如何返回1或0的表,然后计算多个字段名称的相似表中匹配的1,按[Loan Identifier]进行分组。我目前有一个名为dbo.Tape的表,它是原始表,然后是dbo。[Tape Capture]用户正在更新。两个表中的[贷款标识符]相同。
谢谢你能提供帮助。
SELECT
[Loan Identifier], [Completion date], [Tenure], [Underwriter name]
FROM
dbo.Tape
EXCEPT
SELECT
[Loan Identifier], [Completion date], [Tenure], [Underwriter name]
FROM
dbo.[Tape Capture]
如果[Completion date]
中的dbo.Tape
与[Completion date]
中的dbo.[Tape Capture]
匹配,那么我想返回1,否则为0。然后我想在每列中总结所有这些值。例如,
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
然后我想要结果表:
[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
从这里,我想总结每一栏的价值。
答案 0 :(得分:2)
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]