2014-09-19 170 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子句。

相关问题