Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

br_ms_cnes #149

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions models/br_ms_cnes/br_ms_cnes__dicionario.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{ config(alias='dicionario', schema='br_ms_cnes') }}
SELECT
SAFE_CAST(id_tabela AS STRING) id_tabela,
SAFE_CAST(coluna AS STRING) coluna,
SAFE_CAST(chave AS STRING) chave,
SAFE_CAST(cobertura_temporal AS STRING) cobertura_temporal,
SAFE_CAST(valor AS STRING) valor
FROM basedosdados-staging.br_ms_cnes_staging.dicionario AS t
46 changes: 46 additions & 0 deletions models/br_ms_cnes/equipamento.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{
config(
schema='br_ms_cnes',
materialized='table',
partition_by={
"field": "ano",
"data_type": "int64",
"range": {
"start": 2005,
"end": 2023,
"interval": 1}
}
)
}}


WITH raw_cnes_equipamento AS (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
SELECT *
FROM `basedosdados-staging.br_ms_cnes_staging.equipamento`
gabrielle-carv marked this conversation as resolved.
Show resolved Hide resolved
WHERE CNES IS NOT NULL),
cnes_add_muni AS (
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipamento
LEFT JOIN (SELECT id_municipio, id_municipio_6,
FROM `basedosdados-dev.br_bd_diretorios_brasil.municipio`) as mun
ON raw_cnes_equipamento.CODUFMUN = mun.id_municipio_6
)
SELECT
SAFE_CAST(ano AS INT64) AS ano,
SAFE_CAST(mes AS INT64) AS mes,
SAFE_CAST(sigla_uf AS STRING) AS sigla_uf,
SAFE_CAST(id_municipio AS STRING) AS id_municipio,
SAFE_CAST(CNES AS STRING) AS id_estabelecimento_cnes,
SAFE_CAST(CODEQUIP AS STRING) AS id_equipamento,
SAFE_CAST(TIPEQUIP AS STRING) AS tipo_equipamento,
SAFE_CAST(QT_EXIST AS STRING) AS quantidade_equipamentos,
SAFE_CAST(QT_USO AS STRING) AS quantidade_equipamentos_ativos,
SAFE_CAST(IND_SUS AS INT64) AS indicador_equipamento_disponivel_sus,
SAFE_CAST(IND_NSUS AS INT64) AS indicador_equipamento_indisponivel_sus
FROM cnes_add_muni
WHERE (DATE_DIFF(CURRENT_DATE(),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 6
OR DATE_DIFF(DATE(2023,5,1),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 0)


46 changes: 46 additions & 0 deletions models/br_ms_cnes/equipamento_atualizado.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{
config(
schema='br_ms_cnes',
materialized='table',
partition_by={
"field": "ano",
"data_type": "int64",
"range": {
"start": 2005,
"end": 2023,
"interval": 1}
},
post_hook=['REVOKE `roles/bigquery.dataViewer` ON TABLE {{ this }} FROM "specialGroup:allUsers"',
'GRANT `roles/bigquery.dataViewer` ON TABLE {{ this }} TO "group:bd-pro@basedosdados.org"'])
}}


WITH raw_cnes_equipamento AS (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
SELECT *
FROM `basedosdados-staging.br_ms_cnes_staging.equipamento`
WHERE CNES IS NOT NULL),
cnes_add_muni AS (
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipamento
LEFT JOIN (SELECT id_municipio, id_municipio_6,
FROM `basedosdados-dev.br_bd_diretorios_brasil.municipio`) as mun
ON raw_cnes_equipamento.CODUFMUN = mun.id_municipio_6
)
SELECT
SAFE_CAST(ano AS INT64) AS ano,
SAFE_CAST(mes AS INT64) AS mes,
SAFE_CAST(sigla_uf AS STRING) AS sigla_uf,
SAFE_CAST(id_municipio AS STRING) AS id_municipio,
SAFE_CAST(CNES AS STRING) AS id_estabelecimento_cnes,
SAFE_CAST(CODEQUIP AS STRING) AS id_equipamento,
SAFE_CAST(TIPEQUIP AS STRING) AS tipo_equipamento,
SAFE_CAST(QT_EXIST AS STRING) AS quantidade_equipamentos,
SAFE_CAST(QT_USO AS STRING) AS quantidade_equipamentos_ativos,
SAFE_CAST(IND_SUS AS INT64) AS indicador_equipamento_disponivel_sus,
SAFE_CAST(IND_NSUS AS INT64) AS indicador_equipamento_indisponivel_sus
FROM cnes_add_muni



60 changes: 60 additions & 0 deletions models/br_ms_cnes/equipe.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{{
config(
schema='br_ms_cnes',
materialized='table',
partition_by={
"field": "ano",
"data_type": "int64",
"range": {
"start": 2005,
"end": 2023,
"interval": 1}
}
)
}}

WITH raw_cnes_equipe AS (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
SELECT *
FROM `basedosdados-dev.br_ms_cnes_staging.equipe`
WHERE CNES IS NOT NULL),
cnes_add_muni AS (
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipe
LEFT JOIN (SELECT id_municipio, id_municipio_6,
FROM `basedosdados-dev.br_bd_diretorios_brasil.municipio`) as mun
ON raw_cnes_equipe.CODUFMUN = mun.id_municipio_6
)
--tipo_desativacao_equipe com valor 0 que não é indicado como um valor possível do campo no dicionário do cnes.
-- pode ser NA. Em todos os anos tem valor significativo de zeros
--tipo_segmento e descricao_segmento vem juntos na tabela e nao esta presente no dicionario original
SELECT
SAFE_CAST(ano AS INT64) AS ano,
SAFE_CAST(mes AS INT64) AS mes,
SAFE_CAST(sigla_uf AS STRING) AS sigla_uf,
SAFE_CAST(id_municipio AS STRING) AS id_municipio,
SAFE_CAST(CNES AS STRING) AS id_estabelecimento_cnes,
SAFE_CAST(ID_EQUIPE AS STRING) AS id_equipe,
SAFE_CAST(TIPO_EQP AS STRING) AS tipo_equipe,
SAFE_CAST(NOME_EQP AS STRING) AS equipe,
SAFE_CAST(NOMEAREA AS STRING) AS area,
SAFE_CAST(ID_SEGM AS STRING) AS id_segmento,
SAFE_CAST(TIPOSEGM AS STRING) AS tipo_segmento,
SAFE_CAST(DESCSEGM AS STRING) AS descricao_segmento,
--- inserir subsrt para criar ano e mes
SAFE_CAST(SUBSTR(DT_ATIVA, 1, 4) AS INT64) AS ano_ativacao_equipe,
SAFE_CAST(SUBSTR(DT_ATIVA,5,6) AS INT64) AS mes_ativacao_equipe,
SAFE_CAST(MOTDESAT AS STRING) AS motivo_desativacao_equipe,
SAFE_CAST(TP_DESAT AS STRING) AS tipo_desativacao_equipe,
SAFE_CAST(SUBSTR(DT_DESAT, 1, 4) AS INT64) AS ano_desativacao_equipe,
SAFE_CAST(SUBSTR(DT_DESAT,5,6) AS INT64) AS mes_desativacao_equipe,
SAFE_CAST(QUILOMBO AS STRING) AS indicador_atende_populacao_assistida_quilombolas,
SAFE_CAST(ASSENTAD AS STRING) AS indicador_atende_populacao_assistida_assentados,
SAFE_CAST(POPGERAL AS STRING) AS indicador_atende_populacao_assistida_geral,
SAFE_CAST(ESCOLA AS STRING) AS indicador_atende_populacao_assistida_escolares,
SAFE_CAST(INDIGENA AS STRING) AS indicador_atende_populacao_assistida_indigena,
SAFE_CAST(PRONASCI AS STRING) AS indicador_atende_populacao_assistida_pronasci,
FROM cnes_add_muni
WHERE (DATE_DIFF(CURRENT_DATE(),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 6
OR DATE_DIFF(DATE(2023,5,1),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 0)
62 changes: 62 additions & 0 deletions models/br_ms_cnes/equipe_atualizado.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{
config(
schema='br_ms_cnes',
materialized='table',
partition_by={
"field": "ano",
"data_type": "int64",
"range": {
"start": 2005,
"end": 2023,
"interval": 1}
},
post_hook=[
'REVOKE `roles/bigquery.dataViewer` ON TABLE {{ this }} FROM "specialGroup:allUsers"',
'GRANT `roles/bigquery.dataViewer` ON TABLE {{ this }} TO "group:bd-pro@basedosdados.org"'
])
}}


WITH raw_cnes_equipe AS (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
SELECT *
FROM `basedosdados-staging.br_ms_cnes_staging.equipe`
WHERE CNES IS NOT NULL),
cnes_add_muni AS (
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipe
LEFT JOIN (SELECT id_municipio, id_municipio_6,
FROM `basedosdados-dev.br_bd_diretorios_brasil.municipio`) as mun
ON raw_cnes_equipe.CODUFMUN = mun.id_municipio_6
)
--tipo_desativacao_equipe com valor 0 que não é indicado como um valor possível do campo no dicionário do cnes.
-- pode ser NA. Em todos os anos tem valor significativo de zeros
--tipo_segmento e descricao_segmento vem juntos na tabela e nao esta presente no dicionario original
SELECT
SAFE_CAST(ano AS INT64) AS ano,
SAFE_CAST(mes AS INT64) AS mes,
SAFE_CAST(sigla_uf AS STRING) AS sigla_uf,
SAFE_CAST(id_municipio AS STRING) AS id_municipio,
SAFE_CAST(CNES AS STRING) AS id_estabelecimento_cnes,
SAFE_CAST(ID_EQUIPE AS STRING) AS id_equipe,
SAFE_CAST(TIPO_EQP AS STRING) AS tipo_equipe,
SAFE_CAST(NOME_EQP AS STRING) AS equipe,
SAFE_CAST(NOMEAREA AS STRING) AS area,
SAFE_CAST(ID_SEGM AS STRING) AS id_segmento,
SAFE_CAST(TIPOSEGM AS STRING) AS tipo_segmento,
SAFE_CAST(DESCSEGM AS STRING) AS descricao_segmento,
SAFE_CAST(SUBSTR(DT_ATIVA, 1, 4) AS INT64) AS ano_ativacao_equipe,
SAFE_CAST(SUBSTR(DT_ATIVA,5,6) AS INT64) AS mes_ativacao_equipe,
SAFE_CAST(MOTDESAT AS STRING) AS motivo_desativacao_equipe,
SAFE_CAST(TP_DESAT AS STRING) AS tipo_desativacao_equipe,
SAFE_CAST(SUBSTR(DT_DESAT, 1, 4) AS INT64) AS ano_desativacao_equipe,
SAFE_CAST(SUBSTR(DT_DESAT,5,6) AS INT64) AS mes_desativacao_equipe,
SAFE_CAST(QUILOMBO AS STRING) AS indicador_atende_populacao_assistida_quilombolas,
SAFE_CAST(ASSENTAD AS STRING) AS indicador_atende_populacao_assistida_assentados,
SAFE_CAST(POPGERAL AS STRING) AS indicador_atende_populacao_assistida_geral,
SAFE_CAST(ESCOLA AS STRING) AS indicador_atende_populacao_assistida_escolares,
SAFE_CAST(INDIGENA AS STRING) AS indicador_atende_populacao_assistida_indigena,
SAFE_CAST(PRONASCI AS STRING) AS indicador_atende_populacao_assistida_pronasci,
FROM cnes_add_muni

6 changes: 4 additions & 2 deletions models/br_ms_cnes/estabelecimento.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ cnes_add_muni AS (
-- 4. padronização, ordenação de colunas e conversão de tipos
-- 5. Aplica macro clean_cols em certas colunas
SELECT
CAST(SUBSTR(COMPETEN, 1, 4) AS INT64) AS ano,
CAST(SUBSTR(COMPETEN, 5, 2) AS INT64) AS mes,
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,
Expand Down Expand Up @@ -245,3 +245,5 @@ cnes_add_muni AS (
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 AS t
WHERE (DATE_DIFF(CURRENT_DATE(),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 6
OR DATE_DIFF(DATE(2023,5,1),DATE(CAST(ano AS INT64),CAST(mes AS INT64),1), MONTH) > 0)
Loading
Loading