Skip to content

Commit

Permalink
Tuva CC new (#447)
Browse files Browse the repository at this point in the history
* Tuva CC new

* reverted test_date_override to default (false)

* fixing bq error and escaping apostrophe

---------

Co-authored-by: thutuva <101137626+thutuva@users.noreply.github.com>
  • Loading branch information
2 people authored and tuvaforrest committed May 7, 2024
1 parent 84f744c commit f1cf73b
Show file tree
Hide file tree
Showing 5 changed files with 500 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,34 @@
)
}}

with condition_row_number as (

select
patient_id
, normalized_code
, recorded_date
, row_number() over(
partition by
patient_id
, normalized_code
order by recorded_date asc
) as rn_asc
, row_number() over(
partition by
patient_id
, normalized_code
order by recorded_date desc
) as rn_desc
with all_conditions as (
select
patient_id,
normalized_code,
recorded_date
from {{ ref('tuva_chronic_conditions__stg_core__condition') }}
),

)

, patient_conditions as (

select
patient_id
, normalized_code as icd_10_cm
, max(
case
when rn_asc = 1
then recorded_date
end
) as first_diagnosis_date
, max(
case
when rn_desc = 1
then recorded_date
end
) as last_diagnosis_date
from condition_row_number
group by
patient_id
, normalized_code
conditions_with_first_and_last_diagnosis_date as (
select
patient_id,
normalized_code as icd_10_cm,
min(recorded_date) as first_diagnosis_date,
max(recorded_date) as last_diagnosis_date
from all_conditions
group by patient_id, normalized_code

)

select
pc.patient_id
, h.condition_family
, h.condition
, min(first_diagnosis_date) as first_diagnosis_date
, max(last_diagnosis_date) as last_diagnosis_date
, '{{ var('tuva_last_run')}}' as tuva_last_run
from {{ ref('chronic_conditions__tuva_chronic_conditions_hierarchy') }} h
inner join patient_conditions pc
on h.icd_10_cm_code = pc.icd_10_cm
group by
pc.patient_id
, h.condition_family
, h.condition

select
aa.patient_id,
bb.concept_name as condition,
min(first_diagnosis_date) as first_diagnosis_date,
max(last_diagnosis_date) as last_diagnosis_date,
'{{ var('tuva_last_run')}}' as tuva_last_run
from conditions_with_first_and_last_diagnosis_date aa
inner join {{ ref('value_set_member_relevant_fields') }} bb
on aa.icd_10_cm = bb.code
group by aa.patient_id, bb.concept_name
Loading

0 comments on commit f1cf73b

Please sign in to comment.