2015-02-06 84 views
0

我寫了下面的查詢來檢查重複項。檢查空列值的情況下,並忽略它們

select s.*,m.IsMember_Ind, 
     case when REPLACE(s.[ Owner Mobile],' ','') 
        in (select cd.Mobile 
         from Company_Detail cd 
         inner join MEMBERSHIP m 
         on cd.Company_ID = m.Company_ID 
         where m.IsMember_Ind = 1) 
        then 'Match'    
       else ' ' end as OwnerMobileMatch, 
     case when REPLACE(s.[ Contact Number],' ','') 
        in (select cd.Mobile 
         from Company_Detail cd 
         inner join MEMBERSHIP m 
         on cd.Company_ID = m.Company_ID 
         where m.IsMember_Ind = 1) 
        then 'Match' else ' ' end as ContactMobileMatch    
from COMPANY c 
inner join surreynonmembers s 
on c.TTR_ID = s.[Company ID] 
left outer join MEMBERSHIP m 
on m.Company_ID = c.Company_ID 

我的問題是,case語句返回匹配時發現一個空值,我不需要。如果只有一個值,我該如何修改這個查詢來返回?

+0

爲什麼不只是使用另一種情況,或者如果使用ISNULL()函數呢? – sdrzymala 2015-02-06 13:56:24

+0

或者在ContactMobileMatch中包含'AND'S。[Owner Mobile] <>'''在OwnerMobileMatch和'AND s。[Contact Number] <>'''中。 – Tom 2015-02-06 21:44:40

回答

1

你試過這個嗎?

GO 
SELECT s.* 
    ,m.IsMember_Ind 
    ,CASE 
     WHEN REPLACE(s.[ Owner Mobile], ' ', '') IN (
       SELECT cd.Mobile 
       FROM Company_Detail cd 
       INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID 
       WHERE m.IsMember_Ind = 1 
       AND ISNULL(s.[ Owner Mobile], '') <> '' 
       ) 
      THEN 'Match' 
     ELSE ' ' 
     END AS OwnerMobileMatch 
    ,CASE 
     WHEN REPLACE(s.[ Contact Number], ' ', '') IN (
       SELECT cd.Mobile 
       FROM Company_Detail cd 
       INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID 
       WHERE m.IsMember_Ind = 1 
       AND ISNULL(s.[ Contact Number], '')<> '' 
       ) 
      THEN 'Match' 
     ELSE ' ' 
     END AS ContactMobileMatch 
FROM COMPANY c 
INNER JOIN surreynonmembers s ON c.TTR_ID = s.[Company ID] 
LEFT JOIN MEMBERSHIP m ON m.Company_ID = c.Company_ID 

GO