Skip to content

Commit

Permalink
Merge branch 'main' into update-mme-energia
Browse files Browse the repository at this point in the history
  • Loading branch information
tricktx authored Mar 22, 2024
2 parents 1b4d407 + 0f6df6b commit 0307e06
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,87 @@
partition_by={
"field": "ano",
"data_type": "INT64",
"range": {"start": 1935, "end": 2023, "interval": 1},
"range": {"start": 1935, "end": 2024, "interval": 1},
},
)
}}

select
safe_cast(ano as int64) ano,
safe_cast(
split(
format_timestamp('%Y-%m-%dT%H:%M:%E*S', timestamp(dataapresentacao)), 'T'
)[offset(0)] as date
) data,
safe_cast(
split(
format_timestamp('%Y-%m-%dT%H:%M:%E*S', timestamp(dataapresentacao)), 'T'
)[offset(1)] as time
) horario,
safe_cast(id as string) id_proposicao,
safe_cast(uri as string) url,
safe_cast(numero as string) numero,
safe_cast(siglatipo as string) sigla,
safe_cast(descricaotipo as string) tipo,
safe_cast(ementa as string) ementa,
safe_cast(ementadetalhada as string) ementa_detalhada,
safe_cast(keywords as string) palavra_chave,
safe_cast(uriorgaonumerador as string) url_orgao_numerador,
safe_cast(uripropprincipal as string) url_principal,
safe_cast(uripropposterior as string) url_posterior,
safe_cast(urlinteiroteor as string) url_teor_proposicao,
safe_cast(ultimostatus_datahora as string) data_hora_ultimo_status,
safe_cast(ultimostatus_urirelator as string) url_relator_ultimo_status,
safe_cast(ultimostatus_siglaorgao as string) sigla_orgao_ultimo_status,
safe_cast(ultimostatus_regime as string) regime_ultimo_status,
safe_cast(ultimostatus_descricaotramitacao as string) tramitacao_ultimo_status,
safe_cast(ultimostatus_descricaosituacao as string) situacao_ultimo_status,
safe_cast(ultimostatus_despacho as string) despacho_ultimo_status,
safe_cast(ultimostatus_apreciacao as string) apreciacao_ultimo_status,
safe_cast(ultimostatus_sequencia as string) sequencia_ultimo_status,
safe_cast(ultimostatus_url as string) url_ultimo_status,
from `basedosdados-dev.br_camara_dados_abertos_staging.proposicao_microdados` as t
with
table as (
select
safe_cast(ano as int64) ano,
safe_cast(
split(
format_timestamp(
'%Y-%m-%dT%H:%M:%E*S', timestamp(dataapresentacao)
),
'T'
)[offset (0)] as date
) data,
safe_cast(
split(
format_timestamp(
'%Y-%m-%dT%H:%M:%E*S', timestamp(dataapresentacao)
),
'T'
)[offset (1)] as time
) horario,
safe_cast(id as string) id_proposicao,
safe_cast(uri as string) url,
safe_cast(numero as string) numero,
safe_cast(siglatipo as string) sigla,
safe_cast(descricaotipo as string) tipo,
safe_cast(ementa as string) ementa,
safe_cast(ementadetalhada as string) ementa_detalhada,
safe_cast(keywords as string) palavra_chave,
safe_cast(uriorgaonumerador as string) url_orgao_numerador,
safe_cast(uripropprincipal as string) url_principal,
safe_cast(uripropposterior as string) url_posterior,
safe_cast(urlinteiroteor as string) url_teor_proposicao,
safe_cast(ultimostatus_datahora as string) data_hora_ultimo_status,
safe_cast(ultimostatus_urirelator as string) url_relator_ultimo_status,
safe_cast(ultimostatus_siglaorgao as string) sigla_orgao_ultimo_status,
safe_cast(ultimostatus_regime as string) regime_ultimo_status,
safe_cast(
ultimostatus_descricaotramitacao as string
) tramitacao_ultimo_status,
safe_cast(ultimostatus_descricaosituacao as string) situacao_ultimo_status,
safe_cast(ultimostatus_despacho as string) despacho_ultimo_status,
safe_cast(ultimostatus_apreciacao as string) apreciacao_ultimo_status,
safe_cast(ultimostatus_sequencia as string) sequencia_ultimo_status,
safe_cast(ultimostatus_url as string) url_ultimo_status,
from
`basedosdados-dev.br_camara_dados_abertos_staging.proposicao_microdados`
as t
),
query_total as (
select
ano,
case when data >= current_date() then null else data end as data,
horario,
id_proposicao,
url,
numero,
sigla,
tipo,
ementa,
ementa_detalhada,
palavra_chave,
url_orgao_numerador,
url_principal,
url_posterior,
url_teor_proposicao,
data_hora_ultimo_status,
url_relator_ultimo_status,
sigla_orgao_ultimo_status,
regime_ultimo_status,
tramitacao_ultimo_status,
situacao_ultimo_status,
despacho_ultimo_status,
apreciacao_ultimo_status,
sequencia_ultimo_status,
url_ultimo_status,
from table
)
select *
from query_total
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ select
safe_cast(tipo_consumo as string) tipo_consumo,
safe_cast(replace(numero_consumidores, ".0", '') as int64) numero_consumidores,
safe_cast(consumo as int64) consumo

from `basedosdados-dev.br_mme_consumo_energia_eletrica_staging.uf` as t
29 changes: 18 additions & 11 deletions models/br_ms_cnes/br_ms_cnes__estabelecimento.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with
raw_cnes_estabelecimento as (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
select *
from `basedosdados-staging.br_ms_cnes_staging.estabelecimento`
from `basedosdados-dev.br_ms_cnes_staging.estabelecimento`
where cnes is not null
),
raw_cnes_estabelecimento_without_duplicates as (
Expand All @@ -43,8 +43,8 @@ select
safe_cast(ano as int64) as ano,
safe_cast(mes as int64) as mes,
safe_cast(sigla_uf as string) sigla_uf,
cast(substr(dt_atual, 1, 4) as int64) as ano_atualizacao,
cast(substr(dt_atual, 5, 2) as int64) as mes_atualizacao,
safe_cast(substr(cast(dt_atual as string), 1, 4) as int64) as ano_atualizacao,
safe_cast(substr(cast(dt_atual as string), 5, 2) as int64) as mes_atualizacao,
safe_cast(id_municipio as string) id_municipio,
safe_cast(codufmun as string) id_municipio_6,
safe_cast({{ clean_cols("REGSAUDE") }} as string) id_regiao_saude,
Expand Down Expand Up @@ -79,22 +79,28 @@ select
safe_cast(c_corren as string) conta_corrente,
safe_cast(contratm as string) id_contrato_municipio_sus,
safe_cast(
parse_date('%Y%m%d', dt_publm) as date
safe.parse_date('%Y%m%d', cast(dt_publm as string)) as date
) data_publicacao_contrato_municipal,
safe_cast(parse_date('%Y%m%d', dt_puble) as date) data_publicacao_contrato_estadual,
safe_cast(
safe.parse_date('%Y%m%d', cast(dt_puble as string)) as date
) data_publicacao_contrato_estadual,
safe_cast(contrate as string) id_contrato_estado_sus,
safe_cast(alvara as string) numero_alvara,
safe_cast(parse_date('%Y%m%d', dt_exped) as date) data_expedicao_alvara,
safe_cast(
safe.parse_date('%Y%m%d', cast(dt_exped as string)) as date
) data_expedicao_alvara,
safe_cast({{ clean_cols("ORGEXPED") }} as string) tipo_orgao_expedidor,
safe_cast(
{{ clean_cols("AV_ACRED") }} as string
) tipo_avaliacao_acreditacao_hospitalar,
safe_cast(clasaval as string) tipo_classificacao_acreditacao_hospitalar,
cast(substr(dt_acred, 1, 4) as int64) as ano_acreditacao,
cast(substr(dt_acred, 5, 2) as int64) as mes_acreditacao,
safe_cast({{ clean_cols("AV_PNASS") }} as int64) tipo_avaliacao_pnass,
cast(substr(dt_pnass, 1, 4) as int64) as ano_avaliacao_pnass,
cast(substr(dt_pnass, 5, 2) as int64) as mes_avaliacao_pnass,
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,
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,
safe_cast(gesprg1e as int64) indicador_gestao_basica_ambulatorial_estadual,
safe_cast(gesprg1m as int64) indicador_gestao_basica_ambulatorial_municipal,
Expand Down Expand Up @@ -263,6 +269,7 @@ select
safe_cast(ap07cv04 as int64) indicador_atendimento_regulacao_plano_seguro_terceiro,
safe_cast(ap07cv05 as int64) indicador_atendimento_regulacao_plano_saude_publico,
safe_cast(ap07cv06 as int64) indicador_atendimento_regulacao_plano_saude_privado
from cnes_add_muni
{% if is_incremental() %}
where

Expand Down
40 changes: 3 additions & 37 deletions models/br_ms_sia/br_ms_sia__dicionario.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,10 @@
}}


with
dict as (
select
id_tabela,
coluna,
cobertura_temporal,
valor,
case
when id_tabela = 'producao_ambulatorial' and coluna = 'tipo_unidade'
then lpad(chave, 2, '0')
when
id_tabela = 'producao_ambulatorial'
and coluna = 'tipo_financiamento_producao'
then lpad(chave, 2, '0')
when
id_tabela = 'producao_ambulatorial'
and coluna = 'carater_atendimento'
then lpad(chave, 2, '0')
when
id_tabela = 'producao_ambulatorial' and coluna = 'raca_cor_paciente'
then lpad(chave, 2, '0')
when
id_tabela = 'producao_ambulatorial'
and coluna = 'motivo_saida_paciente'
then lpad(chave, 2, '0')
when
id_tabela = 'producao_ambulatorial'
and coluna = 'subtipo_financiamento_producao'
then lpad(chave, 6, '0')
else chave
end as chave2
from basedosdados - dev.br_ms_sia_staging.dicionario
)

select
safe_cast(id_tabela as string) id_tabela,
safe_cast(coluna as string) nome_coluna,
safe_cast(chave2 as string) chave,
safe_cast(nome_coluna as string) nome_coluna,
safe_cast(chave as string) chave,
safe_cast(replace(cobertura_temporal, '-1', '(1)') as string) cobertura_temporal,
safe_cast(valor as string) valor
from dict
from `basedosdados-dev.br_ms_sia_staging.dicionario`
37 changes: 27 additions & 10 deletions models/br_ms_sia/br_ms_sia__producao_ambulatorial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with
from
`basedosdados-dev.br_ms_sia_staging.producao_ambulatorial`
as producao_ambulatorial

left join
(
select id_municipio, id_municipio_6,
Expand All @@ -45,8 +46,8 @@ select
-- ) cnpj_mantenedora_estabalecimento,
-- safe_cast(regexp_replace(pa_cnpj_cc, '0{14}', '') as string) cnpj_orgao,
-- safe_cast(pa_mn_ind as string) tipo_mantenedor_estabelecimento,
safe_cast(pa_gestao as string) id_gestao,
safe_cast(pa_condic as string) tipo_gestao,
-- safe_cast(pa_gestao as string) id_gestao,
-- safe_cast(pa_condic as string) tipo_gestao,
safe_cast(pa_regct as string) tipo_regra_contratual,
safe_cast(pa_ine as string) id_equipe,
safe_cast(pa_srv_c as string) id_servico_especializado,
Expand All @@ -57,8 +58,12 @@ select
regexp_replace(pa_autoriz, '0{13}', '') as string
) codigo_autorizacao_apac,
safe_cast(pa_codoco as string) codigo_ocorrencia,
safe_cast(pa_tpfin as string) tipo_financiamento_producao,
safe_cast(pa_subfin as string) subtipo_financiamento_producao,
case
when pa_tpfin = '00' then '0' else cast(ltrim(pa_tpfin, '0') as string)
end as tipo_financiamento_producao,
case
when pa_subfin = '0000' then '0' else cast(ltrim(pa_subfin, '0') as string)
end as subtipo_financiamento_producao,
-- - parse e criar ano mes data é yyyy-mm
safe_cast(substr(pa_mvm, 1, 4) as int64) as ano_processamento_procedimento,
safe_cast(substr(pa_mvm, 5, 2) as int64) as mes_processamento_procedimento,
Expand Down Expand Up @@ -130,17 +135,27 @@ select
end
) as string
) as cid_causas_associadas_subcategoria,
safe_cast(pa_catend as string) carater_atendimento,
case
when pa_catend = '00' then '0' else cast(ltrim(pa_catend, '0') as string)
end as carater_atendimento,
safe_cast(regexp_replace(pa_munpcn, '9{6}', '') as string) id_paciente_proto,
safe_cast(replace(pa_sexo, '0', '') as string) sexo_paciente,
safe_cast(regexp_replace(pa_idade, '9{3}', '') as int64) idade_paciente,
safe_cast(pa_racacor as string) raca_cor_paciente,
case
when pa_racacor = '00' then '0' else cast(ltrim(pa_racacor, '0') as string)
end as raca_cor_paciente,
safe_cast(ltrim(pa_etnia, '0') as string) etnia_paciente,
safe_cast(idademin as int64) idade_minima_paciente,
safe_cast(idademax as int64) idade_maxima_paciente,
safe_cast(pa_flidade as string) compatibilidade_idade_procedimento,
safe_cast(pa_nivcpl as string) complexidade_procedimento,
safe_cast(pa_docorig as string) instrumento_registro,
case
when pa_flidade = '00' then '0' else cast(ltrim(pa_flidade, '0') as string)
end as compatibilidade_idade_procedimento,
case
when pa_nivcpl = '00' then '0' else cast(ltrim(pa_nivcpl, '0') as string)
end as complexidade_procedimento,
case
when pa_docorig = '00' then '0' else cast(ltrim(pa_docorig, '0') as string)
end as instrumento_registro,
safe_cast(pa_valapr as float64) valor_aprovado_procedimento,
safe_cast(pa_qtdapr as int64) quantidade_aprovada_procedimento,
safe_cast(pa_valpro as float64) valor_produzido_procedimento,
Expand All @@ -151,7 +166,9 @@ select
safe_cast(pa_vl_cf as float64) valor_complemento_federal,
safe_cast(pa_vl_cl as float64) valor_complemento_local,
safe_cast(pa_vl_inc as float64) valor_incremento,
safe_cast(pa_motsai as string) motivo_saida_paciente,
case
when pa_motsai = '00' then '0' else cast(ltrim(pa_motsai, '0') as string)
end as motivo_saida_paciente,
-- - em uf e muicipio replace de
safe_cast(
regexp_replace(pa_ufdif, '9{1}', '') as int64
Expand Down
Loading

0 comments on commit 0307e06

Please sign in to comment.