2014-09-19 2 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가 null이기 때문에 나는 그것을 반환되지 말았어야 테이블의 값을 확인하고 SQL에 null가 아닌 currency_cd 상태. 또한 평가 날짜 2014년 7월 10일

표 값 이후 : 절을 필터링에 사용되는

MatterStatus_CD: 
Active   
AssessedDate: 
7/24/2014 
Currency_CD: 
NULL 
EndDate: 
NULL 
+0

'Currency_CD IS NOT NULL'조건은 '왼쪽 결합'을위한'on' 절에 있습니다. 따라서 필터가 원하는대로 작동하지 않고 조인 조건으로 작동합니다. –

+0

'ME.Currency_CD IS NOT NULL'IS NOT NULL 부분은 where 절로 이동해야합니다. –

답변

0

, 절에 가입하지. 모든 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

+0

모의 데이터를 [sql fiddle] (http://sqlfiddle.com/)에 넣으십시오. –

0

당신은 MatterMatterExposure에 합류 남아 있습니다. LEFT JOIN의 ON 문에서 Me.Currency_CD IS NOT NULL 기준은 "Currency_CD가 null 인 레코드에 참여하지 마십시오."라고 말하고 있지만 왼쪽 조인이기 때문에 조인에서 테이블의 레코드 중 하나도 손실되지 않습니다 (Matter). 따라서 Currency_CD을 요청하면 NULL이 표시됩니다.

Currency_CDMatterExposure 테이블에 널 (null)입니다 (대신 가입의) 당신이 어디 성명에서 Currency_CD IS NOT NULL를 지정해야합니다 중 쿼리에서 레코드를 제거하거나 INNER이 MatterExposure 테이블에 가입 사용해야합니다.

0

이 조인은 LEFT OUTER JOIN이기 때문입니다. 이 조인을 적용하려면 내부 조인을 수행하십시오. 또는 WHERE 절로 이동하십시오.

관련 문제