diff --git a/models/br_ms_cnes/br_ms_cnes__estabelecimento.sql b/models/br_ms_cnes/br_ms_cnes__estabelecimento.sql index 8e0327ec..761eac6e 100644 --- a/models/br_ms_cnes/br_ms_cnes__estabelecimento.sql +++ b/models/br_ms_cnes/br_ms_cnes__estabelecimento.sql @@ -56,51 +56,47 @@ select -- fazer replace em valores de linha com 14 zeros para null. 14 zeros é o tamanho -- de um cpf/cnpj nulo safe_cast(regexp_replace(cnpj_man, '0{14}', '') as string) cnpj_mantenedora, - replace( - safe_cast({{ clean_cols("COD_IR") }} as string), 'nan', null - ) tipo_retencao_tributos_mantenedora, + case + when safe_cast({{ clean_cols("COD_IR") }} as string) = 'nan' + then null + else safe_cast({{ clean_cols("COD_IR") }} as string) + end as tipo_retencao_tributos_mantenedora, safe_cast(vinc_sus as int64) indicador_vinculo_sus, safe_cast(tpgestao as string) tipo_gestao, - replace( - safe_cast({{ clean_cols("ESFERA_A") }} as string), 'nan', null - ) tipo_esfera_administrativa, - replace( - safe_cast( - case - when regexp_replace(safe_cast(retencao as string), '^0+', '') = '' - then '0' - else regexp_replace(safe_cast(retencao as string), '^0+', '') - end as string - ), - 'nan', - null - ) as tipo_retencao_tributos, + case + when safe_cast({{ clean_cols("ESFERA_A") }} as string) = 'nan' + then null + else safe_cast({{ clean_cols("ESFERA_A") }} as string) + end as tipo_esfera_administrativa, + case + when regexp_replace(safe_cast(retencao as string), '^0+', '') = '' + then '0' + when safe_cast(retencao as string) = 'nan' + then null + else regexp_replace(safe_cast(retencao as string), '^0+', '') + end as tipo_retencao_tributos, safe_cast({{ clean_cols("ATIVIDAD") }} as string) tipo_atividade_ensino_pesquisa, - replace( - safe_cast( - case - when regexp_replace(safe_cast(natureza as string), '^0+', '') = '' - then '0' - else regexp_replace(safe_cast(natureza as string), '^0+', '') - end as string - ), - 'nan', - null - ) as tipo_natureza_administrativa, + case + when regexp_replace(safe_cast(natureza as string), '^0+', '') = '' + then '0' + when safe_cast(natureza as string) = 'nan' + then null + else regexp_replace(safe_cast(natureza as string), '^0+', '') + end as tipo_natureza_administrativa, nullif(safe_cast(nat_jur as string), '') id_natureza_juridica, - replace( - safe_cast( - case - when regexp_replace(safe_cast(clientel as string), '^0+', '') = '' - then '0' - else regexp_replace(safe_cast(clientel as string), '^0+', '') - end as string - ), - 'nan', - null - ) as tipo_fluxo_atendimento, + case + when regexp_replace(safe_cast(clientel as string), '^0+', '') = '' + then '0' + when safe_cast(clientel as string) = 'nan' + then null + else regexp_replace(safe_cast(clientel as string), '^0+', '') + end as tipo_fluxo_atendimento, safe_cast({{ clean_cols("TP_UNID") }} as string) tipo_unidade, - replace(safe_cast({{ clean_cols("TURNO_AT") }} as string), 'nan', null) tipo_turno, + case + when safe_cast({{ clean_cols("TURNO_AT") }} as string) = 'nan' + then null + else safe_cast({{ clean_cols("TURNO_AT") }} as string) + end as tipo_turno, safe_cast({{ clean_cols("NIV_HIER") }} as string) tipo_nivel_hierarquia, safe_cast({{ clean_cols("TP_PREST") }} as string) tipo_prestador, safe_cast(co_banco as string) banco, @@ -118,28 +114,23 @@ select safe_cast( safe.parse_date('%Y%m%d', cast(dt_exped as string)) as date ) data_expedicao_alvara, - replace( - safe_cast({{ clean_cols("ORGEXPED") }} as string), 'nan', null - ) tipo_orgao_expedidor, - replace( - safe_cast({{ clean_cols("AV_ACRED") }} as string), 'nan', null - ) tipo_avaliacao_acreditacao_hospitalar, - replace( - safe_cast( - case - when regexp_replace(safe_cast(clasaval as string), '^0+', '') = '' - then '0' - else regexp_replace(safe_cast(clasaval as string), '^0+', '') - end as string - ), - 'nan', - null - ) as tipo_classificacao_acreditacao_hospitalar, - safe_cast(substr(cast(dt_acred as string), 1, 4) as int64) as ano_acreditacao, - safe_cast(substr(cast(dt_acred as string), 5, 2) as int64) as mes_acreditacao, - safe_cast( - cast({{ clean_cols("AV_PNASS") }} as string) as int64 - ) tipo_avaliacao_pnass, + case + when safe_cast({{ clean_cols("ORGEXPED") }} as string) = 'nan' + then null + else safe_cast({{ clean_cols("ORGEXPED") }} as string) + end as tipo_orgao_expedidor, + case + when safe_cast({{ clean_cols("AV_ACRED") }} as string) = 'nan' + then null + else safe_cast({{ clean_cols("AV_ACRED") }} as string) + end as tipo_avaliacao_acreditacao_hospitalar, + case + when regexp_replace(safe_cast(clasaval as string), '^0+', '') = '' + then '0' + when safe_cast(clasaval as string) = 'nan' + then null + else regexp_replace(safe_cast(clasaval as string), '^0+', '') + end as tipo_classificacao_acreditacao_hospitalar, safe_cast(substr(cast(dt_pnass as string), 1, 4) as int64) as ano_avaliacao_pnass, safe_cast(substr(cast(dt_pnass as string), 5, 2) as int64) as mes_avaliacao_pnass, safe_cast(nivate_a as int64) indicador_atencao_ambulatorial, diff --git a/models/br_ms_cnes/br_ms_cnes__profissional.sql b/models/br_ms_cnes/br_ms_cnes__profissional.sql index 48f90986..73abc4f3 100644 --- a/models/br_ms_cnes/br_ms_cnes__profissional.sql +++ b/models/br_ms_cnes/br_ms_cnes__profissional.sql @@ -43,9 +43,15 @@ select -- UFMUNRES que indica null safe_cast(regexp_replace(ufmunres, '0{6}', '') as string) id_municipio_6_residencia, safe_cast(nomeprof as string) nome, - safe_cast(replace(vinculac, 'nan', null) as string) tipo_vinculo, - safe_cast(replace(registro, 'nan', null) as string) id_registro_conselho, - safe_cast(replace(conselho, 'nan', null) as string) tipo_conselho, + case + when vinculac = 'nan' then null else safe_cast(vinculac as string) + end as tipo_vinculo, + case + when registro = 'nan' then null else safe_cast(registro as string) + end as id_registro_conselho, + case + when conselho = 'nan' then null else safe_cast(conselho as string) + end as tipo_conselho, -- replace de valores de linha com 15 zeros para null. 15 zeros é valor do campo -- CNS_PROF que indica null safe_cast(regexp_replace(cns_prof, '0{15}', null) as string) cartao_nacional_saude,