Quantcast
Channel: Active questions tagged cte - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 207

creating a CTE to filter previous CTE populaiton down. How to find new diagnosis of certain ICD 10 code?

$
0
0

The first CTE grabs all patients with the diagnosis codes for substance abuse, the second CTE should filter down to only NEW paitents.Meaning of the people in the first CTE, which of thses people have noy been diagnosised with the 2 codes before. The 3rd CTE grabs all information about the person, locations, date, and age.

In need of figuring out the logic for the 2nd CTE: from diag ... only people who havent been diagnosed before (ie. new/inital diagnosis)

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_idleft 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_Diagnosisfrom (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_idinner join diag on e.encntr_id = diag.encntr_idinner join (select distinct * from DEMOGRAPHICS where mrn_rownum = 1 and phone_rownum = 1 and address_rownum = 1) dem on e.person_id = dem.person_idinner join locations_amb amb on amb.loc_facility_cd = e.loc_facility_cdwhere age_in_years(e.beg_effective_dt_tm::date, dem.dob) > 13 

Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>