2016-10-11 14 views
0

我有一個CTE,它給了我760行的結果。我還有另外一個SELECT聲明給我722行。 我想查看在CTE中存在的哪些記錄不存在於SELECT聲明中。 我使用NOT EXISTS聲明,但由於某種原因,它不給我任何結果。我也嘗試過NOT IN - 但是沒有記錄。如何查找來自同一個源表的兩個查詢之間不共享的行

;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 
    ) 

enter image description here

從CTE只有760行的結果看起來是這樣的: enter image description here

而且從SELECT語句,讓722行的結果看起來是這樣的: enter image description here

我已經這樣做了「

; 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 

現在我有888行min_tag = 2和max_tag = 2。這是否意味着每個保單號碼都被複制到我的源表中? enter image description here

+0

不太熟悉TSQL,但可能它成爲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收集他們,並找到該組合的唯一項目。

; 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) 

max(tag) = 1只在CTE。

+0

我做到了。現在我有888行,min_tag = 2和max_tag = 2。那麼這是否意味着表'CatalyticWindEQ'中的每個保單號重複兩次? – Oleg

+0

我在上面添加了結果 – Oleg

+0

如果min_tag = 2,那麼該行不會被CTE返回。所有這2種方法都是在「工會」聲明的第二部分。 – cco

0

你可以使用全外連接標誌的差異,把你的第二個查詢另一個CTE裏面後,說CTE2,你可以嘗試這樣的事:

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重複。你需要仔細檢查。 –

相關問題