Skip to content

Commit

Permalink
Merge pull request #216 from basedosdados/staging/br_ms_cnes
Browse files Browse the repository at this point in the history
br_ms_cnes
  • Loading branch information
folhesgabriel authored Aug 7, 2023
2 parents ac9ba4b + 3fccfd8 commit 86c0162
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 16 deletions.
4 changes: 3 additions & 1 deletion models/br_ms_cnes/equipamento.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ WITH raw_cnes_equipamento AS (
FROM `basedosdados-dev.br_ms_cnes_staging.equipamento`
WHERE CNES IS NOT NULL),
cnes_add_muni AS (
-- 3. Adicionar id_municipio
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipamento
LEFT JOIN (SELECT id_municipio, id_municipio_6,
Expand All @@ -40,5 +40,7 @@ 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:[email protected]"'])
}}


WITH raw_cnes_equipamento AS (
-- 1. Retirar linhas com id_estabelecimento_cnes nulo
SELECT *
FROM `basedosdados-dev.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



5 changes: 3 additions & 2 deletions models/br_ms_cnes/equipe.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
)
}}


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 (
-- 3. Adicionar id_municipio
-- 2. Adicionar id_municipio de 7 dígitos
SELECT *
FROM raw_cnes_equipe
LEFT JOIN (SELECT id_municipio, id_municipio_6,
Expand Down Expand Up @@ -57,3 +56,5 @@ 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)
63 changes: 63 additions & 0 deletions models/br_ms_cnes/equipe_atualizado.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{
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:[email protected]"'
])
}}


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

4 changes: 2 additions & 2 deletions models/br_ms_cnes/estabelecimento.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ raw_cnes_estabelecimento_without_duplicates as(
FROM raw_cnes_estabelecimento
),
cnes_add_muni AS (
-- 3. Adicionar id_municipio
-- 3. Adicionar id_municipio e sigla_uf
SELECT *
FROM raw_cnes_estabelecimento_without_duplicates
LEFT JOIN (SELECT id_municipio, id_municipio_6,
LEFT JOIN (SELECT id_municipio, id_municipio_6, sigla_uf,
FROM `basedosdados-dev.br_bd_diretorios_brasil.municipio`) as mun
ON raw_cnes_estabelecimento_without_duplicates.CODUFMUN = mun.id_municipio_6
)
Expand Down
Loading

0 comments on commit 86c0162

Please sign in to comment.