나는 760 행의 결과를 제공하는 CTE를 가지고 있습니다. 그리고 나는 또 다른 SELECT
문장을 가지고 722 행을 제공합니다. SELECT
문에 존재하지 않는 CTE 레코드가 있는지 확인하고 싶습니다. 나는 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
)
는 760 행 CTE의 결과는 다음과 같습니다
![enter image description here](https://i.stack.imgur.com/8eJuQ.png)
그리고 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 개의 행이 있습니다. 각 정책 번호가 원본 테이블에 복제된다는 의미입니까?
너무 익숙하지 않은,하지만 그것을 할 수 NOT EXISTS 절에 GROUP BY가 있습니까? GROUP BY가 어떤 일을하기 위해 집계 함수를 사용하고있는 것처럼 보이지 않습니다. – James
그냥 댓글을 달아서 다시 시도했지만 문제는 남아 있습니다. 감사합니다 – Oleg
['EXCEPT'] (https://msdn.microsoft.com/en-us/library/ms188055.aspx)? – HABO