2014-09-19 206 views
0

誰能告訴我爲什麼這個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 
+0

'Currency_CD IS NOT NULL'條件在'left join'的'on'子句中。因此,它並不像您想要的那樣充當過濾器,它充當了連接條件。 – 2014-09-19 16:04:51

+0

'ME.Currency_CD IS NOT NULL' IS NOT NULL部分應該移動到where子句 – 2014-09-19 16:05:00

回答

0

Where子句用於過濾,而不是加入條款。每個JOIN都應該有一個ON。這是一個良好的開端。

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 
Left Outer Join Entity E on MP.Entity_EID = E.Entity_EID 
Left Outer Join Communication C on MP.Entity_EID = C.Entity_EID 
Left Outer Join MatterExposure ME on M.MatterNumber_Id = ME.MatterNumber_Id 


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%')     
and MP.Role_Cd In ('Primary Lawyer', 'Primary Staff Member') 
and MP.EndDate is Null 
and ME.AssessedDate > '7/10/2014' 
and ME.Currency_CD IS NOT NULL 
and C.CommunicationType_Cd = 'Email' 

Order by Email, M.MatterName 
+0

當我這樣做時,現在沒有結果返回。 – codingNightmares 2014-09-19 18:43:11

+0

將模擬數據放入[sql小提琴](http://sqlfiddle.com/),我將更多地混淆它 – 2014-09-19 19:25:08

0

你是左加入MatterExposureMatter。在LEFT JOIN的ON語句中,您的Me.Currency_CD IS NOT NULL條件僅表示「不加入Currency_CD爲空的記錄...」,但由於這是一個左連接,因此您的Matter表記錄在連接中都不會丟失。所以當你要求Currency_CD時,你會得到NULL

從查詢中刪除記錄,其中Currency_CD是在MatterExposure表空你要麼需要在WHERE語句指定Currency_CD IS NOT NULL(而不是你的加入)或將需要使用一個INNER JOIN你MatterExposure表。

0

這是因爲這個連接是一個左外連接。如果您想強制執行此連接,請將其作爲內連接。或將該檢查移至WHERE子句。

相關問題