66
77with first_last as (
88 select person_id
9- ,payer
10- ,data_source
11- ,{{ quote_column(' plan' ) }}
12- ,cast(left(year_month,4 ) as int ) as year_nbr
13- ,min (year_month) as first_month
14- ,max (year_month) as last_month
9+ , payer
10+ , data_source
11+ , {{ quote_column(' plan' ) }}
12+ , cast(left(year_month, 4 ) as int ) as year_nbr
13+ , min (year_month) as first_month
14+ , max (year_month) as last_month
1515 from {{ ref(' core__member_months' ) }}
16- group by
16+ group by
1717 person_id
18- ,data_source
19- ,payer
20- ,cast(left(year_month,4 ) as int )
21- ,{{ quote_column(' plan' ) }}
18+ , data_source
19+ , payer
20+ , cast(left(year_month, 4 ) as int )
21+ , {{ quote_column(' plan' ) }}
2222)
2323
24- , enrollment_fields as
25- (
24+ , enrollment_fields as (
2625select distinct
2726 encounter_id
28- ,payer
29- ,{{ quote_column(' plan' ) }}
30- ,person_id
31- ,data_source
27+ , payer
28+ , {{ quote_column(' plan' ) }}
29+ , person_id
30+ , data_source
3231from {{ ref(' core__medical_claim' ) }}
3332)
3433
35- ,enrollment_row as
36- (
37- select encounter_id
38- ,payer
39- ,{{ quote_column( ' plan ' ) }}
40- ,person_id
41- ,data_source
42- ,row_number() over (partition by encounter_id order by payer
43- ,{{ quote_column(' plan' ) }}
44- ,person_id
45- ,data_source
34+ , enrollment_row as (
35+ select encounter_id
36+ , payer
37+ , {{ quote_column( ' plan ' ) }}
38+ , person_id
39+ , data_source
40+ , row_number() over (partition by encounter_id
41+ order by payer
42+ , {{ quote_column(' plan' ) }}
43+ , person_id
44+ , data_source
4645 ) as row_num
4746from enrollment_fields
4847)
@@ -51,38 +50,38 @@ from enrollment_fields
5150select
5251 e .encounter_id
5352 , e .data_source
54- , coalesce(p .sex ,' unknown' ) as sex
53+ , coalesce(p .sex , ' unknown' ) as sex
5554 , c .year as year_nbr
5655 , e .length_of_stay
5756 , e .discharge_disposition_code
58- , case
59- when rs .index_admission_flag = 1
60- and rs .unplanned_readmit_30_flag = 1 then 1
61- else 0
57+ , case
58+ when rs .index_admission_flag = 1
59+ and rs .unplanned_readmit_30_flag = 1 then 1
60+ else 0
6261 end as readmission_numerator
63- , coalesce(rs .index_admission_flag ,0 ) as readmission_denominator
64- , coalesce(st_ab .ansi_fips_state_name ,st_full .ansi_fips_state_name ,' null_state' ) as state
65- , case
66- when r .description is not null then r .description
62+ , coalesce(rs .index_admission_flag , 0 ) as readmission_denominator
63+ , coalesce(st_ab .ansi_fips_state_name , st_full .ansi_fips_state_name , ' null_state' ) as state
64+ , case
65+ when r .description is not null then r .description
6766 else ' null_race'
6867 end as race
6968 , case when e .drg_code_type = ' ms-drg' then e .drg_code else ' null_drg' end as ms_drg_code
7069 , coalesce(ccsr .default_ccsr_category_description_ip , ' unknown' ) as ccsr_cat
7170 , datediff(year, p .birth_date , e .encounter_start_date ) as age_at_admit_with_null
72- , case
71+ , case
7372 when e .discharge_disposition_code = ' 01' then ' home'
7473 when e .discharge_disposition_code = ' 06' then ' home health'
7574 when e .discharge_disposition_code = ' 03' then ' snf'
7675 when e .discharge_disposition_code = ' 20' then ' expired'
7776 when e .discharge_disposition_code = ' 62' then ' ipt rehab'
78- when e .discharge_disposition_code in (' 02' , ' 63' , ' 13' , ' 12' , ' 05' , ' 04' , ' 61' , ' 65' , ' 64' )
77+ when e .discharge_disposition_code in (' 02' , ' 63' , ' 13' , ' 12' , ' 05' , ' 04' , ' 61' , ' 65' , ' 64' )
7978 then ' transfer/other facility'
80- when e .discharge_disposition_code in (' 50' , ' 51' ) then ' hospice'
81- else ' other'
79+ when e .discharge_disposition_code in (' 50' , ' 51' ) then ' hospice'
80+ else ' other'
8281 end as discharge_location
8382
8483 , fl .first_month
85- , fl .last_month
84+ , fl .last_month
8685 /* tuva chronic conditions */
8786 , pc .hip_fracture as cond_hip_fracture
8887 , pc .type_1_diabetes_mellitus as cond_type_1_diabetes_mellitus
@@ -322,48 +321,48 @@ select
322321 , phcc .hcc_186
323322 , phcc .hcc_188
324323 , phcc .hcc_189
325-
326- from {{ ref(' core__encounter' )}} e
327- inner join {{ ref(' reference_data__calendar' )}} c on e .encounter_start_date = c .full_date
328- inner join {{ ref(' core__patient' )}} p on e .person_id = p .person_id
329- inner join {{ ref(' benchmarks__pivot_condition' ) }} pc on e .person_id = pc .person_id
324+
325+ from {{ ref(' core__encounter' ) }} as e
326+ inner join {{ ref(' reference_data__calendar' ) }} as c on e .encounter_start_date = c .full_date
327+ inner join {{ ref(' core__patient' ) }} as p on e .person_id = p .person_id
328+ inner join {{ ref(' benchmarks__pivot_condition' ) }} as pc on e .person_id = pc .person_id
330329 and
331330 c .year = pc .year_nbr
332- inner join {{ ref(' benchmarks__pivot_cms_condition' ) }} pcms on e .person_id = pcms .person_id
331+ inner join {{ ref(' benchmarks__pivot_cms_condition' ) }} as pcms on e .person_id = pcms .person_id
333332 and
334333 c .year = pcms .year_nbr
335- inner join {{ ref(' benchmarks__pivot_hcc' ) }} phcc on e .person_id = phcc .person_id
334+ inner join {{ ref(' benchmarks__pivot_hcc' ) }} as phcc on e .person_id = phcc .person_id
336335 and
337- c .year = phcc .year_nbr
338- inner join enrollment_row er on e .encounter_id = er .encounter_id
336+ c .year = phcc .year_nbr
337+ inner join enrollment_row as er on e .encounter_id = er .encounter_id
339338 and
340339 er .row_num = 1
341- inner join first_last fl on e .person_id = fl .person_id
340+ inner join first_last as fl on e .person_id = fl .person_id
342341 and
343342 er.{{ quote_column(' plan' ) }} = fl.{{ quote_column(' plan' ) }}
344- and
343+ and
345344 er .payer = fl .payer
346345 and
347346 er .data_source = fl .data_source
348347 and
349348 fl .year_nbr = c .year
350- left join {{ ref(' ccsr__dxccsr_v2023_1_cleaned_map' ) }} ccsr on e .primary_diagnosis_code = ccsr .icd_10_cm_code
351- left join {{ ref(' reference_data__ansi_fips_state' )}} st_ab on p .state = st_ab .ansi_fips_state_abbreviation
352- left join {{ ref(' reference_data__ansi_fips_state' )}} st_full on p .state = st_full .ansi_fips_state_name
353- left join {{ ref(' terminology__race' )}} r on p .race = r .description
354- left join {{ ref(' readmissions__readmission_summary' )}} rs on e .encounter_id = rs .encounter_id
349+ left outer join {{ ref(' ccsr__dxccsr_v2023_1_cleaned_map' ) }} as ccsr on e .primary_diagnosis_code = ccsr .icd_10_cm_code
350+ left outer join {{ ref(' reference_data__ansi_fips_state' ) }} as st_ab on p .state = st_ab .ansi_fips_state_abbreviation
351+ left outer join {{ ref(' reference_data__ansi_fips_state' ) }} as st_full on p .state = st_full .ansi_fips_state_name
352+ left outer join {{ ref(' terminology__race' ) }} as r on p .race = r .description
353+ left outer join {{ ref(' readmissions__readmission_summary' ) }} as rs on e .encounter_id = rs .encounter_id
355354where e .encounter_type = ' acute inpatient'
356355)
357356
358- ,handle_missing_age as (
357+ , handle_missing_age as (
359358 select data_source
360- ,avg (age_at_admit_with_null) as avg_age_at_admit
359+ , avg (age_at_admit_with_null) as avg_age_at_admit
361360 from final
362361 group by data_source
363362)
364363
365364select final.*
366- ,coalesce(age_at_admit_with_null,h .avg_age_at_admit ) as age_at_admit
365+ , coalesce(age_at_admit_with_null, h .avg_age_at_admit ) as age_at_admit
367366, ' {{ var(' tuva_last_run' ) }}' as tuva_last_run
368367from final
369- inner join handle_missing_age h on final .data_source = h .data_source
368+ inner join handle_missing_age as h on final .data_source = h .data_source
0 commit comments