为什么此SQL查询返回此结果?

时间:2014-09-19 16:02:41

标签: sql sql-server

任何人都可以告诉我为什么这个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, 'l.w@abc.ca;m.j@abc.com') 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 i中检查表中的值,不应该返回它,因为Currency_CD为null,而在sql中,它表示currency_cd不为null。评估日期也是2014年7月10日之后

表值:

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

3 个答案:

答案 0 :(得分:0)

where子句用于过滤,而不是join子句。每个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, 'l.w@abc.ca;m.j@abc.com') 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

答案 1 :(得分:0)

您将MatterExposure左侧加入Matter。 LEFT JOIN' ON语句中的Me.Currency_CD IS NOT NULL条件仅表示,"不加入Currency_CD为空的记录..."但因为这是一个左连接,所以Matter表的记录都不会在连接中丢失。因此,当您要求Currency_CD时,您将获得NULL

要从Currency_CD表中MatterExposure为空的查询中删除记录,您需要在where语句中指定Currency_CD IS NOT NULL(而不是您的联接)或需要对你的MatterExposure表使用INNER JOIN。

答案 2 :(得分:0)

这是因为这个连接是LEFT OUTER JOIN。如果要强制执行此连接,请将其设置为内连接。或者将该检查移至WHERE子句。

相关问题