如何在where子句中过滤组合键?

时间:2018-10-19 21:42:14

标签: sql sql-server tsql where-clause dml

我正在尝试使用复合键过滤查询,但是,我不确定如何在where子句中使用复合键过滤查询。 XSiteProductItemIdMercuryProductItemId组成了一个标识唯一行的复合键。

这是我的查询:

SELECT VMPOrdersExtract.XSiteProductItemId
  ,  VMPOrdersExtract.MercuryProductItemId
FROM Appraisal.ext.VMPOrders AS VMPOrdersExtract
LEFT JOIN dw.DimAppraisal DimApprasial ON DimApprasial.XsiteProductItemId = VMPOrdersExtract.XSiteProductItemId
  AND DimApprasial.MercuryProductItemId = VMPOrdersExtract.MercuryProductItemId
WHERE VMPOrdersExtract.LastSyncDateTime > DimApprasial.LastSyncDateTime
OR (
    VMPOrdersExtract.XSiteProductItemId
    AND VMPOrdersExtract.MercuryProductItemId
) is null

很明显,我在OR子句中的WHERE条件在语法上是错误的。如何仅过滤null表中不存在的DimAppraisal组合键记录?

2 个答案:

答案 0 :(得分:1)

怎么样

WHERE VMPOrdersExtract.LastSyncDateTime > DimApprasial.LastSyncDateTime
OR (VMPOrdersExtract.XSiteProductItemId IS NULL AND 
VMPOrdersExtract.MercuryProductItemId IS NULL);

答案 1 :(得分:1)

也将其添加到答案中;)

 SELECT VMPOrdersExtract.XSiteProductItemId, VMPOrdersExtract.MercuryProductItemId 
FROM Appraisal.ext.VMPOrders AS VMPOrdersExtract LEFT JOIN dw.DimAppraisal DimApprasial 
ON DimApprasial.XsiteProductItemId = VMPOrdersExtract.XSiteProductItemId AND 
DimApprasial.MercuryProductItemId = VMPOrdersExtract.MercuryProductItemId WHERE 
VMPOrdersExtract.LastSyncDateTime > DimApprasial.LastSyncDateTime OR 
(VMPOrdersExtract.XSiteProductItemId is null AND VMPOrdersExtract.MercuryProductItemId is null)