Skip to content

Commit

Permalink
Merge branch 'hci/fix/alergias' into hci/review/thiago
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiagoTrabach committed Aug 27, 2024
2 parents f08b154 + 0d6d920 commit 8d00922
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ with
from {{ ref("raw_prontuario_vitai__alergia") }}

),
paciente_mrg as (
select cpf, c as cns
from {{ ref("mart_historico_clinico__paciente")}}, unnest(cns) as c
),
alergias_std as (
select
gid,
Expand Down Expand Up @@ -81,10 +85,15 @@ with
select distinct
gid_paciente,
if(cns in ('', 'None'), null, cns) as cns,
if(cpf in ('', 'None'), null, cpf) as cpf
from {{ ref("raw_prontuario_vitai__boletim") }}
if(cpf in ('', 'None'), null, cpf) as cpf,
from {{ ref("raw_prontuario_vitai__boletim") }}
)

select alergias_agg.gid_paciente as id_paciente, cns, cpf, alergias
select
alergias_agg.gid_paciente as id_paciente,
boletim.cns,
IF(boletim.cpf is null,paciente_mrg.cpf,boletim.cpf) as cpf,
alergias
from alergias_agg
left join boletim on boletim.gid_paciente = alergias_agg.gid_paciente
left join paciente_mrg on boletim.cns = paciente_mrg.cns
29 changes: 9 additions & 20 deletions models/marts/historico_clinico/mart_historico_clinico__alergia.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@ with
select *
from vitai
union all
select *
from vitacare
),

final as (
select
cns as paciente_cns,
cpf as paciente_cpf,
array_agg(alergia) as alergias,
struct(current_timestamp() as created_at) as metadados
from total
group by id_paciente, cns, cpf
having {{ validate_cpf("cpf") }} or cns is not null
select * from vitacare
)
select
cpf as paciente_cpf,
array_agg(distinct alergia) as alergias,
array_agg(distinct cns ignore nulls) as cns,
safe_cast(current_datetime() as datetime) as processed_at
from total
where cpf is not null
group by cpf

-- select paciente_cpf, count(1) as records
-- from final
-- group by 1
-- having records > 1
select *
from final
where paciente_cpf is null

0 comments on commit 8d00922

Please sign in to comment.