-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: spliting indice table into indice and busca
- Loading branch information
1 parent
52873c2
commit e2003f4
Showing
2 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
models/marts/dit/historico_clinico_app/mart_historico_clinico_app__busca.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{{ | ||
config( | ||
alias="busca", | ||
schema="app_historico_clinico", | ||
materialized="table", | ||
cluster_by="nome", | ||
partition_by={ | ||
"field": "cpf_particao", | ||
"data_type": "int64", | ||
"range": {"start": 0, "end": 100000000000, "interval": 34722222}, | ||
}, | ||
) | ||
}} | ||
|
||
with | ||
source_paciente_mart as ( | ||
select | ||
* | ||
from {{ ref('mart_historico_clinico__paciente') }} | ||
), | ||
source_paciente_app as ( | ||
select | ||
* | ||
from {{ ref('mart_historico_clinico_app__paciente') }} | ||
), | ||
source_episodio_app as ( | ||
select | ||
* | ||
from {{ ref('mart_historico_clinico_app__episodio') }} | ||
), | ||
|
||
-- ----------------------------------------- | ||
-- Enriquecimento | ||
-- ----------------------------------------- | ||
cns_mapeamento as ( | ||
select | ||
cpf, | ||
array_agg(valor_cns) as cns_lista | ||
from source_paciente_mart, unnest(cns) as valor_cns | ||
group by 1 | ||
), | ||
nome_mae_mapeamento as ( | ||
select | ||
cpf, | ||
dados.mae_nome | ||
from source_paciente_mart | ||
), | ||
episodios_por_pessoas as ( | ||
select | ||
source_episodio_app.cpf, | ||
count(*) as quantidade_episodios | ||
from source_episodio_app | ||
group by 1 | ||
), | ||
|
||
-- ----------------------------------------- | ||
-- Dados do paciente | ||
-- ----------------------------------------- | ||
dados as ( | ||
select | ||
cast(source_paciente_app.cpf as int64) as cpf_particao, | ||
|
||
source_paciente_app.cpf, | ||
cns_lista, | ||
|
||
registration_name as nome, | ||
birth_date as data_nascimento, | ||
{{ calculate_age('cast(birth_date as date)') }} AS idade, | ||
gender as genero, | ||
nome_mae_mapeamento.mae_nome as nome_mae, | ||
coalesce(quantidade_episodios, 0) as quantidade_episodios, | ||
|
||
exibicao | ||
from source_paciente_app | ||
inner join cns_mapeamento on cns_mapeamento.cpf = source_paciente_app.cpf | ||
inner join nome_mae_mapeamento on nome_mae_mapeamento.cpf = source_paciente_app.cpf | ||
left join episodios_por_pessoas on episodios_por_pessoas.cpf = source_paciente_app.cpf | ||
) | ||
-- -=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-- | ||
-- FINAL | ||
-- -=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-- | ||
select * | ||
from dados | ||
where {{ validate_cpf("cpf") }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters