2013-04-25 3 views
0

#TempTable에서 준비 테이블에없는 모든 레코드를 찾으려고합니다. 비교가 16 개 필드 이상에서 이루어져야한다는 점에 유의해야합니다.여러 열을 사용하여 존재하지 않는다

여러 조합을 시도했지만 아무 것도 작동하지 않는 것 같습니다.

SELECT CustomerAccountNo FROM #TempTable   
    WHERE NOT EXISTS 
    (SELECT e.[CustomerAccountNo] , 
    e.[MeterNo] , 
    e.[CustomerName1] , 
    e.[ServiceAddress1] , 
    e.[ServiceAddress2] , 
    e.[ServiceCity] , 
    e.[ServiceState] , 
    e.[ServiceZip] , 
    e.[BillingAddress1] , 
    e.[BillingAddress2] , 
    e.[BillingAddress3] , 
    e.[BillingCity] , 
    e.[BillingState] , 
    e.[BillingZip] , 
    e.[BillingZip4] , 
    e.[PrimaryPhoneNumber] FROM #TempTable e 
    JOIN dbo.Staging s 
    ON e.CustomerAccountNo = s.CustomerAccountNo AND 
    e.MeterNo = s.MeterNo AND 
    e.CustomerName1 = s.CustomerName1 AND 
    e.ServiceAddress1 = s.ServiceAddress1 AND 
    e.ServiceAddress2 = s.ServiceAddress2 AND 
    e.ServiceCity = s.ServiceCity AND 
    e.ServiceState = s.ServiceState AND 
    e.ServiceZip = s.ServiceZip AND 
    e.BillingAddress1 = s.BillingAddress1 AND 
    e.BillingAddress2 = s.BillingAddress2 AND 
    e.BillingAddress3 = s.BillingAddress3 AND 
    e.BillingCity = s.BillingCity AND 
    e.BillingState = s.BillingState AND 
    e.BillingZip = s.BillingZip AND 
    e.BillingZip4 = s.BillingZip4 AND 
    e.PrimaryPhoneNumber= s.PrimaryPhoneNumber   
    ) 
+0

어떻게이 작동하지 않습니다

이 하나의 시도

? 너무 많은 기록을 남기고 있습니까? 부족한? 또는...? – Melanie

+0

이 코드 조합에서 모든 레코드가 다시 반환됩니다. 모든게 빠진 것처럼. 다른 사람들은 아무것도 얻지 못합니다 ... –

+0

@HABO - 그들은 거기에서 왔습니다. #TempTable e JOIN dbo.Staging s – Hogan

답변

2

대신의 JOIN처럼 Except.

SELECT CustomerAccountNo, MeterNo -- and so on 
FROM #TempTable 
EXCEPT 
SELECT CustomerAccountNo, MeterNo -- and so on 
FROM Staging 
0

그냥 가입하고 null을 찾으십시오. 이

SELECT e.* 
FROM #TempTable e 
LEFT JOIN dbo.Staging s 
    ON e.CustomerAccountNo = s.CustomerAccountNo AND 
    e.MeterNo = s.MeterNo AND 
    e.CustomerName1 = s.CustomerName1 AND 
    e.ServiceAddress1 = s.ServiceAddress1 AND 
    e.ServiceAddress2 = s.ServiceAddress2 AND 
    e.ServiceCity = s.ServiceCity AND 
    e.ServiceState = s.ServiceState AND 
    e.ServiceZip = s.ServiceZip AND 
    e.BillingAddress1 = s.BillingAddress1 AND 
    e.BillingAddress2 = s.BillingAddress2 AND 
    e.BillingAddress3 = s.BillingAddress3 AND 
    e.BillingCity = s.BillingCity AND 
    e.BillingState = s.BillingState AND 
    e.BillingZip = s.BillingZip AND 
    e.BillingZip4 = s.BillingZip4 AND 
    e.PrimaryPhoneNumber= s.PrimaryPhoneNumb 
    WHERE s.CustomerAccountNo is null 
+0

그것은 OUTER 조인이되어야합니다. 그렇지 않으면 어떻게 ON 절이 작동 할 수 있습니까? –

+0

@AaronBertrand - 네, 나는 왼쪽을 버렸습니다. (왼쪽에 뭔가 남겨 두어야한다면 짐작할 수 있습니다.) – Hogan

0

당신은 정확한 답을 얻을 수있는 자세한 상황을 제공해야 사용해보십시오.

분명히 FROM 절과 WHERE 절 사이에 연결이 없으므로 쿼리에서 모두를 반환합니다.

SELECT CustomerAccountNo FROM #TempTable t  
WHERE NOT EXISTS 
    (SELECT 1 FROM dbo.Staging s WHERE 
    t.CustomerAccountNo = s.CustomerAccountNo AND 
    t.MeterNo = s.MeterNo AND 
    t.CustomerName1 = s.CustomerName1 AND 
    t.ServiceAddress1 = s.ServiceAddress1 AND 
    t.ServiceAddress2 = s.ServiceAddress2 AND 
    t.ServiceCity = s.ServiceCity AND 
    t.ServiceState = s.ServiceState AND 
    t.ServiceZip = s.ServiceZip AND 
    t.BillingAddress1 = s.BillingAddress1 AND 
    t.BillingAddress2 = s.BillingAddress2 AND 
    t.BillingAddress3 = s.BillingAddress3 AND 
    t.BillingCity = s.BillingCity AND 
    t.BillingState = s.BillingState AND 
    t.BillingZip = s.BillingZip AND 
    t.BillingZip4 = s.BillingZip4 AND 
    t.PrimaryPhoneNumber= s.PrimaryPhoneNumber   
    ) 
+0

where 절은 16 개의 필드 중 1 개만을 고려합니다. 모든 16 개의 필드가 "NOT IN"인 것을 확인해야합니다. –

+0

이 복사하여 붙여 넣기를 수정했으면 다시 시도하십시오. – Robin

관련 문제