记录存在于两个表中,但随着where子句消失

时间:2015-02-20 16:56:52

标签: sql sql-server-2008-r2

下表列出了记录" FF Studios"但是当我运行查询时,我没有看到任何记录。

Select *
from ( Select Name,
              Ref_name,
              Final_Name = CASE
                           WHEN _Similarity >=0.80
                            and _Match ='Solid Matched'
                            and Owner IS NOT NULL
                           THEN Ref_Name
                           ELSE Name
                           END, 
              Address,
              City,
              Province,
              [Postal Code],
              Country,
              Phone,
              Fax,
              Owner
              _Similarity,
              _Match
       from Customer_Stage3
     ) as a
WHERE a.Final_Name LIKE ('%FF St%')
  and ( a.Final_Name IN ( Select Customer_Clean 
                          from SFCustomerMerge 
                          Group BY Customer_Clean 
                          HAVING COUNT(Customer_Clean) > 1
                        ) 
      )
order by a._Similarity

在这种情况下,记录存在于SFCustomerMerge表和Customer Stage 3中,但是当我添加第一个Where子句时它会消失并且我已经检查了查询

Select Customer_Clean
from SFCustomerMerge 
Group BY Customer_Clean 
HAVING COUNT(Customer_Clean) > 1

记录也出现在那里。

我错过了什么?我也知道此记录的相似度为0.98,匹配为Solid Matched且Owner不为NULL。

2 个答案:

答案 0 :(得分:0)

它是WHERE子句中的额外括号吗?你试过了吗?

WHERE a.Final_Name IN (SELECT Customer_Clean
                       FROM SFCustomerMerge
                       GROUP BY Customer_Clean
                       HAVING COUNT(Customer_Clean) > 1)

其他一切看起来都很好,所以这是我能看到的唯一可能导致它的东西。

答案 1 :(得分:0)

我首先看一下使用中的校对序列。区分大小写可能会在这里发挥作用。进一步......嵌入式空白怎么样?它完全一样吗?

但是,出于性能原因,我可能会将您的查询重构为以下内容:

select cs3.Name,
       cs3.Ref_name,
       cs3.Final_Name = case
                          when _Similarity >=0.80
                           and _Match ='Solid Matched'
                           and Owner IS NOT NULL
                          then Ref_Name
                          else Name
                        end, 
       cs3.Address,
       cs3.City,
       cs3.Province,
       cs3.[Postal Code],
       cs3.Country,
       cs3.Phone,
       cs3.Fax,
       cs3.Owner
       cs3._Similarity,
       cs3._Match
from Customer_State3 cs3
join ( Select cm.Customer_Clean 
       from SFCustomerMerge cm
       where cm.Customer_Clean like '%FF St%'
       Group BY cm.Customer_Clean 
       HAVING COUNT( cm.Customer_Clean ) > 1
     ) dupes on dupes.Customer_Clean = case
                                         when cs3._Similarity >= 0.80
                                          and cs3._Match       = 'Solid Matched'
                                          and cs3.Owner        is not NULL
                                         then cs3.Ref_Name
                                         else cs3.Name
                                       end = dupes.Customer_Clean
order by cs3._Similarity