I have a CTE that that gives me the result of 760 rows. And I have another SELECT
statement that gives me 722 rows.
I want to see which records exist in CTE that are not exists in SELECT
statement.
I'm using NOT EXISTS
statement, but for some reason it doesnt give me any result. I also tried NOT IN
- but the same...no records.
;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
)
The result from CTE only with 760 rows looks like this:
And the result from SELECT statement that gives 722 rows looks like this:
I've done this"
; 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
And now I have 888 rows with min_tag = 2 and max_tag=2. Does it mean each policy number is duplicated in my source table?