2016-10-11 4 views
0

나는 760 행의 결과를 제공하는 CTE를 가지고 있습니다. 그리고 나는 또 다른 SELECT 문장을 가지고 722 행을 제공합니다. SELECT 문에 존재하지 않는 CTE 레코드가 있는지 확인하고 싶습니다. 나는 NOT EXISTS 성명을 사용하고 있습니다. 그러나 어떤 이유로 그것은 어떤 결과도주지 않습니다. 나는 또한 NOT IN을 시도했다. 그러나 같은 기록은 없다. 동일한 원본 테이블의 두 쿼리간에 공유되지 않는 행을 찾는 방법

enter image description here

;WITH Cte_Policies AS 
     (
     SELECT 
      PolicyNumber, 
      ControlNo, 
      EffectiveDate, 
      ExpirationDate, 
      ProducerName, 
      SUM(BOUND_Premium) as NetWrittenPremium 
     FROM CatalyticWindEQ 
     WHERE EffectiveDate >= '05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE())  
       AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry' AND PolicyNumber IS NOT NULL 
     GROUP BY 

       ProducerName, 
       EffectiveDate 
       ,ExpirationDate ,PolicyNumber, ControlNo 
     ) 
SELECT PolicyNumber, 
     ControlNo, 
     YEAR(EffectiveDate) as PolicyEffectiveYear, 
     MONTH(EffectiveDate) as PolicyEffectiveMonth, 
     NetWrittenPremium, 
     ProducerName as Producer 


FROM 
    Cte_Policies 


    where 
    NOT EXISTS 

    (
     SELECT 
        PolicyNumber 
     FROM  CatalyticWindEQ eq 
     WHERE  EffectiveDate>='05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE()) AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry' 
        AND PolicyNumber IS NOT NULL 
        and eq.PolicyNumber=Cte_Policies.PolicyNumber 
     GROUP BY PolicyNumber 
    ) 
는 760 행 CTE의 결과는 다음과 같습니다 enter image description here

: enter image description here

그리고 722 개 행을 제공 SELECT 문의 결과는 다음과 같다 나는 이것을했다 "

; with CTE as 
(
     SELECT 
      PolicyNumber, 
      ControlNo, 
      EffectiveDate, 
      ExpirationDate, 
      ProducerName, 
      SUM(BOUND_Premium) as NetWrittenPremium 
     FROM CatalyticWindEQ 
     WHERE EffectiveDate >= '05-01-2016' AND EffectiveDate <= EOMONTH(GETDATE())  
       AND LineName = 'Earthquake' AND Underwriter <> 'Batcheller, Jerry' AND PolicyNumber IS NOT NULL 
     GROUP BY 

       ProducerName, 
       EffectiveDate 
       ,ExpirationDate ,PolicyNumber, ControlNo 
     ) 
SELECT PolicyNumber, 

     min(tag) as min_tag, 
     max(tag) as max_tag 


FROM 
     (
     SELECT PolicyNumber, 1 as tag FROM CTE 
    UNION ALL 
     SELECT PolicyNumber, 2 as tag FROM CatalyticWindEQ 
     ) U 
GROUP BY PolicyNumber 
HAVING COUNT(*)=1 

이제 min_tag = 2 및 max_tag = 2 인 888 개의 행이 있습니다. 각 정책 번호가 원본 테이블에 복제된다는 의미입니까? enter image description here

+0

너무 익숙하지 않은,하지만 그것을 할 수 NOT EXISTS 절에 GROUP BY가 있습니까? GROUP BY가 어떤 일을하기 위해 집계 함수를 사용하고있는 것처럼 보이지 않습니다. – James

+0

그냥 댓글을 달아서 다시 시도했지만 문제는 남아 있습니다. 감사합니다 – Oleg

+1

['EXCEPT'] (https://msdn.microsoft.com/en-us/library/ms188055.aspx)? – HABO

답변

0

기본 전략은 두 데이터 세트 (PolicyNumber의 목록)를 선택 union all를 사용하여 수집하고, 조합의 고유 항목을 찾을 수 있습니다. max(tag) = 1을 가진

; with CTE as (...) -- the CTE from above 
Select PolicyNumber, min(tag) as min_tag, max(tag) as max_tag from (
    select PolicyNumber, 1 as tag from CTE 
    union all 
    select PolicyNumber, 2 as tag from CatalyticWindEQ -- the source you're matching 
) U 
Group by PolicyNumber 
Having count(*) =1 -- equivalently, having min(tag) = max(tag) 

행은 CTE 만입니다.

+0

나는 그것을했다. 이제 min_tag = 2 및 max_tag = 2 인 888 개의 행이 있습니다. 즉,'CatalyticWindEQ' 테이블의 각 정책 번호가 두 번 반복된다는 의미입니까? – Oleg

+0

위의 결과를 – Oleg

+0

에 추가했습니다. min_tag가 2 인 경우 행은 CTE에서 반환하지 않습니다. 이 2 가지 의미는 모두 그것이'union' 문장의 두 번째 부분에 있다는 것입니다. – cco

0

당신이 차이 플래그에 조인 완전 외부를 사용할 수있는 또 다른 CTE 내에서 두 번째 쿼리를 가하고 후, cte2, 당신은이 같은 시도 할 수 있습니다 말 : TSQL와

select 
    a.PolicyNumber, 
    a.ControlNo, 
    a.YEAR(EffectiveDate) as PolicyEffectiveYear, 
    a.MONTH(EffectiveDate) as PolicyEffectiveMonth, 
    a.NetWrittenPremium, 
    a.ProducerName as Producer, 
    b.PolicyNumber 
from Cte_Policies as a 
full outer join cte2 as b ON b.PolicyNumber=a.PolicyNumber 
where 
     a.PolicyNumber is null -- will show records NOT in cte. 
    OR b.PolicyNumber is null -- Will show records NOT in cte2. 
+0

또한 빈 테이블을 제공합니다. 그들은 둘 다 똑같은 PolicyNember가 있다는 것을 의미합니까 ?? – Oleg

+0

레코드가 없으면 모든 정책 번호가 쿼리의 양측에서 공통적이라는 것을 의미합니다. 하지만 당신은 cte와 cte2의 쿼리에 대해 서로 다른 카운트를 가지고 있기 때문에 당신은 그룹핑을하고 있습니다, 나는 당신이 몇몇 policyNumber duplicate를 가지고 있다고 생각합니다. 당신은 두 번 확인해야합니다. –