誰能告訴我爲什麼這個sql查詢返回這個結果。這個SQL查詢爲什麼返回這個結果?
SELECT
M.MatterNumber_Id, M.MatterNumber, M.MatterName,
ISNULL(MP.Role_Cd, 'No Primary') AS PrimaryRole,
ISNULL(E.Name, 'No Primary') AS PrimaryName,
ISNULL(C.CommNumber, '[email protected];[email protected]') AS Email
FROM
Matter M
LEFT OUTER JOIN
MatterPlayer MP ON M.MatterNumber_Id = MP.MatterNumber_Id AND
MP.Role_Cd IN ('Primary Lawyer', 'Primary Staff Member') AND
MP.EndDate IS NULL
LEFT OUTER JOIN
Entity E on MP.Entity_EID = E.Entity_EID
LEFT OUTER JOIN Communication C on MP.Entity_EID = C.Entity_EID AND
C.CommunicationType_Cd = 'Email'
LEFT OUTER JOIN
MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id AND
ME.AssessedDate > '7/10/2014' AND
ME.Currency_CD IS NOT NULL
WHERE
M.MatterStatus_Cd = 'Active' AND
ME.AssessedDate IS NULL AND
M.Matter_Cd in
(SELECT
rpl.Type_CD
FROM
RuleProfile_Tabs rpt
INNER JOIN
RuleProfile_LookupCode rpl ON rpt.RuleProfile_ID = rpl.RuleProfile_ID
WHERE
tab_id = 1034 AND Caption LIKE 'Reportable Matter%')
ORDER BY
Email, M.MatterName
但是當我看到結果時,我檢查返回的記錄之一,它不應該被返回。
結果之一返回: 貝利,理查德
在db我檢查值不應該已返回表中,因爲Currency_CD爲空,並在SQL它規定currency_cd不爲空。另外,評估日期爲2014年7月10日後
表值:
MatterStatus_CD:
Active
AssessedDate:
7/24/2014
Currency_CD:
NULL
EndDate:
NULL
'Currency_CD IS NOT NULL'條件在'left join'的'on'子句中。因此,它並不像您想要的那樣充當過濾器,它充當了連接條件。 – 2014-09-19 16:04:51
'ME.Currency_CD IS NOT NULL' IS NOT NULL部分應該移動到where子句 – 2014-09-19 16:05:00