我在查询时遇到了一些问题,以检查具有不同行的2个相同表之间的差异。 这是查询
SELECT *
FROM [PROD01].[myDefDB].[forward].[fv] as DB01
WHERE TargetDate = '20150429' and
NOT EXISTS (SELECT *
FROM [PROD02].[myDefDB].[forward].[fv] as DB02
WHERE DB02.TargetDate = '20150429' and
DB02.Id_Fw = DB01.Id_Fw and
DB02.Id_Bl = DB01.Id_Bl and
DB02.Id_Pt = DB01.Id_Pt and
DB02.TargetDate = DB01.TargetDate and
DB02.StartDate = DB01.EndDate and
DB02.EndDate = DB01.EndDate and
DB02.[Version] = DB01.[Version]
)
考虑[PROD02].[myDefDB].[forward].[fv]
是[PROD01].[myDefDB].[forward].[fv]
的子集,在SELECT count(*)
的两个表上执行TargetDate = '20150429'
会返回2367和4103,所以我希望得到1736从那个查询,但我得到超过2000。
我考虑了WHERE
子句中的所有PK。我错过了什么?
答案 0 :(得分:3)
您可以像这样使用EXCEPT
。
SELECT Id_Fw,Id_Bland,Id_Pt,TargetDate,StartDate,EndDate,[Version]
FROM [PROD01].[myDefDB].[forward].[fv] as DB01
WHERE TargetDate = '20150429'
EXCEPT
SELECT Id_Fw,Id_Bl,Id_Pt,TargetDate,StartDate,EndDate,[Version]
FROM [PROD02].[myDefDB].[forward].[fv] as DB02
WHERE TargetDate = '20150429'
这将获取PROD01
中不在PROD02