1515-- number of rows as the stg_encounter model, i.e.,
1616-- there is a one to one correspondence between the
1717-- rows in stg_encounter and this CTE.
18- with encounter_data_quality_issues as (
18+ with best_encounter as (
19+ select distinct encounter_id
20+ from {{ ref(' readmissions__encounter_overlap' ) }} as overlap
21+ where overlap .is_best_encounter = 0
22+ )
23+
24+ , encounter_data_quality_issues as (
1925select
2026 aa .encounter_id
2127 , case
5561 end as no_diagnosis_ccs_flag
5662 , aa .ccs_diagnosis_category as diagnosis_ccs
5763 , case /* WHEN ENCOUNTER IS NOT THE BEST, THEN FLAG THIS ERROR. */
58- when exists (select 1 from {{ ref(' readmissions__encounter_overlap' ) }} as overlap
59- where aa .encounter_id = overlap .encounter_id and overlap .is_best_encounter = 0 )
64+ when be .encounter_id is not null
6065 then 1
6166 else 0
6267 end as overlaps_with_another_encounter_flag
@@ -78,6 +83,8 @@ from {{ ref('readmissions__encounter_with_ccs') }} as aa
7883 left outer join {{ ref(' terminology__apr_drg' ) }} as dd
7984 on aa .drg_code = dd .apr_drg_code
8085 and aa .drg_code_type = ' apr-drg'
86+ left outer join best_encounter as be
87+ on aa .encounter_id = be .encounter_id
8188)
8289
8390
0 commit comments