我的主查询 (COPD) 中有一组患者,而我的子查询 (CNC) 中有另一组患者。我想从主要查询结果中排除 CANC PAT_ID,但这似乎不起作用并且运行时间太长。有没有更好的方法来排除子查询结果?我尝试了 NOT EXISTS 和 NOT IN,但认为我做的不正确,因为应该被排除在外的患者仍然出现。
SELECT DISTINCT
pe.PAT_ENC_CSN_ID,
pe.PAT_ID,
pe.CONTACT_DATE,
vp.PAT_MRN_ID,
vp.PAT_NAME,
vp.SEX_NAME,
pat.BIRTH_DATE,
vp.AGE_YEARS,
vp.CUR_PCP_NAME
FROM PAT_ENC pe
INNER JOIN V_PAT_FACT vp on pe.PAT_ID=vp.PAT_ID
INNER JOIN PATIENT pat on vp.PAT_ID=pat.PAT_ID
INNER JOIN CLARITY_ADT adt on pe.PAT_ENC_CSN_ID=adt.PAT_ENC_CSN_ID
LEFT OUTER JOIN PAT_ENC_DX dx on pe.PAT_ID=dx.PAT_ID
LEFT OUTER JOIN CLARITY_EDG edg on dx.DX_ID=edg.DX_ID
INNER JOIN GROUPER_COMPILED_RECORDS gcr on edg.DX_ID=gcr.COMPILED_REC_LIST_VALUE
------- EXCLUSION CANCER
LEFT JOIN
(
SELECT DISTINCT pl.PAT_ID
FROM PROBLEM_LIST pl
LEFT OUTER JOIN CLARITY_EDG edg on pl.DX_ID=edg.DX_ID
INNER JOIN GROUPER_COMPILED_RECORDS rec on edg.DX_ID=rec.COMPILED_REC_LIST_VALUE
WHERE rec.GROUPER_ID in ('2100000011')
)cx on pe.PAT_ID=cx.PAT_ID
WHERE pe.CONTACT_DATE > '2016-07-01 00:00:00.000'
AND pe.WEIGHT>= '1587.3' -- 45 kg or more
AND vp.AGE_YEARS BETWEEN '40' AND '80'
AND vp.SEX_C in ('1','2') --FEMALE or MALE
AND adt.PAT_CLASS_C in ('101','103','104') ---IP, OBS or ED
AND vp.IS_VALID_PAT_YN = 'Y' -- NOT TEST
AND pat.PAT_STATUS_C <>'2' --NOT DECEASED
AND cx.PAT_ID IS NULL