创建 CTE 以过滤掉以前的 CTE 数量。如何找到某个ICD 10代码的新诊断?

问题描述 投票:0回答:0

第一个CTE抓住了所有具有药物滥用诊断代码的患者,

第二个 CTE 应该将其过滤到仅新患者。第一个 CTE 中的人的含义,这些人中哪些人现在已经被诊断出具有这 2 个代码。

第三个 CTE 获取有关人员、位置、日期和年龄的所有信息。

需要弄清楚第二个 CTE 的逻辑:从诊断开始......仅限以前未诊断过的人(即新/初步诊断)

with diag as (
select distinct d.person_id
, d.encntr_id
, d.diagnosis_id
, d.nomenclature_id
, d.diag_dt_tm
, d.diag_prsnl_name
, d.diagnosis_display
, cv.display as Source_Vocabulary 
, nm.source_identifier as ICD_10_Code
, nm.source_string as ICD_10_Diagnosis
, nm.concept_cki 
from wny_prod.diagnosis d 
inner join (select * from nomenclature
        where source_vocabulary_cd in (151782752, 151782747) --ICD 10 CM and PCS
        and (source_identifier ilike 'F10.10' or source_identifier ilike 'F19.10')) nm on d.nomenclature_id = nm.nomenclature_id
left join code_value cv on nm.source_vocabulary_cd = cv.code_value 
)

diag_final as (
select * 
from diag
--?
)

select distinct ea.FIN
, dem.patient_name
, dem.dob
, age_in_years(e.beg_effective_dt_tm::date, dem.dob) as Age_at_Encounter
, amb.location_name
, e.beg_effective_dt_tm
, diag.Source_Vocabulary 
, diag.ICD_10_Code
, diag.ICD_10_Diagnosis
from (select * from encounter where year(beg_effective_dt_tm) = 2022) e 
left join (select distinct encntr_id, alias as FIN from encntr_alias where encntr_alias_type_cd = 844) ea on e.encntr_id = ea.encntr_id
inner join diag on e.encntr_id = diag.encntr_id
inner join (select distinct * from DEMOGRAPHICS where mrn_rownum = 1 and phone_rownum = 1 and address_rownum = 1) dem on e.person_id = dem.person_id
inner join locations_amb amb on amb.loc_facility_cd = e.loc_facility_cd
where age_in_years(e.beg_effective_dt_tm::date, dem.dob) > 13 
sql snowflake-cloud-data-platform common-table-expression
© www.soinside.com 2019 - 2024. All rights reserved.