-
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.
- Loading branch information
1 parent
b0bee90
commit a674a0d
Showing
6 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
queries/models/protecao_social_cadunico_hashed/endereco.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,114 @@ | ||
-- CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.endereco` AS | ||
|
||
WITH endereco_tb AS ( | ||
SELECT | ||
id_familia, | ||
CONCAT( | ||
CASE | ||
WHEN tipo_logradouro IS NULL THEN "" | ||
WHEN SAFE_CAST(tipo_logradouro AS INT64) IS NOT NULL THEN "" | ||
ELSE TRIM(REGEXP_REPLACE(REGEXP_REPLACE(tipo_logradouro, r'\s+', ' '), r'[0-9]', '')) | ||
END, | ||
" ", | ||
CASE | ||
WHEN titulo_logradouro IS NULL THEN "" | ||
WHEN SAFE_CAST(titulo_logradouro AS INT64) IS NOT NULL THEN "" | ||
ELSE TRIM(REGEXP_REPLACE(REGEXP_REPLACE(titulo_logradouro, r'\s+', ' '), r'[0-9]', '')) | ||
END, | ||
" ", | ||
CASE | ||
WHEN logradouro IS NULL THEN "" | ||
ELSE TRIM(REGEXP_REPLACE(logradouro, r'\s+', ' ')) | ||
END, | ||
" ", | ||
CASE | ||
WHEN numero_logradouro IS NULL THEN "S/N" | ||
ELSE CAST(numero_logradouro AS STRING) | ||
END, | ||
", RIO DE JANEIRO, RJ, BRASIL" | ||
) AS address, | ||
CASE | ||
WHEN localidade IS NULL THEN "" | ||
WHEN SAFE_CAST(localidade AS INT64) IS NOT NULL THEN "" | ||
ELSE TRIM(REGEXP_REPLACE(REGEXP_REPLACE(localidade, r'\s+', ' '), r'[0-9]', '')) | ||
END AS bairro_cadunico, | ||
REGEXP_REPLACE( | ||
REGEXP_REPLACE( | ||
LOWER(NORMALIZE(CASE | ||
WHEN localidade IS NULL THEN "" | ||
WHEN SAFE_CAST(localidade AS INT64) IS NOT NULL THEN "" | ||
ELSE TRIM(REGEXP_REPLACE(REGEXP_REPLACE(localidade, r'\s+', ' '), r'[0-9]', '')) | ||
END, NFD)), r'[^a-z0-9]+', ''), | ||
r'\s+', ' ' | ||
) AS bairro_limpo_cadunico, | ||
FROM `rj-smas.protecao_social_cadunico.identificacao_controle` | ||
), | ||
|
||
address_tb AS ( | ||
SELECT | ||
distinct | ||
id_familia, | ||
REGEXP_REPLACE(address, r'\s+', ' ') AS address, | ||
bairro_cadunico, | ||
bairro_limpo_cadunico, | ||
FROM endereco_tb | ||
), | ||
|
||
address_prep_tb AS ( | ||
SELECT | ||
id_familia, | ||
address, | ||
SPLIT(address, ',') AS address_array, | ||
bairro_cadunico, | ||
bairro_limpo_cadunico, | ||
FROM address_tb | ||
), | ||
|
||
bairro AS ( | ||
SELECT | ||
id_bairro, | ||
bairro_limpo, | ||
bairro, | ||
subprefeitura, | ||
FROM `rj-smas.protecao_social_cadunico.bairro` | ||
), | ||
|
||
endereco_normalizado AS ( | ||
SELECT | ||
id_familia, | ||
address AS endereco, | ||
REGEXP_REPLACE( | ||
CONCAT( | ||
address_array[OFFSET(0)], | ||
', ', | ||
CASE | ||
WHEN b.bairro IS NULL THEN "" | ||
ELSE UPPER(b.bairro) | ||
END, | ||
', ', | ||
ARRAY_TO_STRING(ARRAY(SELECT x FROM UNNEST(address_array) x WITH OFFSET o WHERE o > 0), ', ') | ||
), r',\s*,',"," | ||
) AS address, | ||
b.id_bairro, | ||
b.bairro AS bairro, | ||
b.bairro_limpo, | ||
a.bairro_cadunico, | ||
a.bairro_limpo_cadunico, | ||
b.subprefeitura, | ||
FROM address_prep_tb a | ||
LEFT JOIN bairro b | ||
ON a.bairro_limpo_cadunico = b.bairro_limpo | ||
) | ||
|
||
--, endereco_geolocalizado AS ( | ||
SELECT | ||
-- count(distinct end_geo.address) | ||
id_familia, | ||
ibge.CD_SETOR AS id_setor_censitario | ||
FROM endereco_normalizado end_norm | ||
INNER JOIN rj-smas.protecao_social_cadunico.endereco_geolocalizado end_geo ON end_norm.address = end_geo.address AND end_geo.geometry IS NOT NULL | ||
INNER JOIN `rj-escritorio-dev.dados_mestres_staging.ibge_setor_censitario_2022` ibge ON ST_Within(ST_GEOGPOINT(end_geo.longitude, end_geo.latitude), ST_GEOGFROMTEXT(ibge.geometry)) | ||
-- ) | ||
-- SELECT | ||
-- id_familia | ||
-- FROM endereco_normalizado |
15 changes: 15 additions & 0 deletions
15
queries/models/protecao_social_cadunico_hashed/escolaridade.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,15 @@ | ||
CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.escolaridade` AS | ||
SELECT | ||
id_familia, | ||
id_membro_familia, | ||
ano_serie_frequenta, | ||
ultimo_ano_serie_frequentou, | ||
id_inep_escola, | ||
id_concluiu_curso_frequentado, | ||
curso_frequenta, | ||
curso_mais_elevado_frequentou, | ||
id_sabe_ler_escrever, | ||
escola_nao_tem_inep, | ||
frequenta_escola, | ||
FROM `rj-smas.protecao_social_cadunico.escolaridade` | ||
where data_particao >= "2024-07-01" |
25 changes: 25 additions & 0 deletions
25
queries/models/protecao_social_cadunico_hashed/familia.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,25 @@ | ||
CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.familia` AS | ||
SELECT | ||
id_familia, | ||
familia_indigena, | ||
familia_quilombola, | ||
pessoas_internadas_0_17_anos, | ||
pessoas_internadas_18_59_anos, | ||
pessoas_internadas_mais_60_anos, | ||
pessoas_domicilio, | ||
despesa_agua_esgoto, | ||
despesa_alimentacao, | ||
despesa_aluguel, | ||
despesa_energia, | ||
despesa_gas, | ||
despesa_medicamentos, | ||
despesa_transporte, | ||
nao_tem_despesa_agua_esgoto, | ||
nao_tem_despesa_alimentacao, | ||
nao_tem_despesa_aluguel, | ||
nao_tem_despesa_energia, | ||
nao_tem_despesa_gas, | ||
nao_tem_despesa_medicamentos, | ||
nao_tem_despesa_transporte, | ||
FROM `rj-smas.protecao_social_cadunico.familia` | ||
where data_particao >= "2024-07-01" |
25 changes: 25 additions & 0 deletions
25
queries/models/protecao_social_cadunico_hashed/identificacao_membro.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,25 @@ | ||
CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.identificacao_membro` AS | ||
with raw_table AS | ||
( | ||
SELECT | ||
id_membro_familia, | ||
id_raca_cor, | ||
id_sexo, | ||
DATE_DIFF(CURRENT_DATE, CAST(data_nascimento AS DATE), YEAR) idade, | ||
data_particao | ||
FROM `rj-smas.protecao_social_cadunico.identificacao_membro` | ||
WHERE data_particao = "2024-07-01" | ||
AND id_estado_cadastral != "4" | ||
) | ||
SELECT | ||
* EXCEPT(idade), | ||
CASE | ||
WHEN idade < 12 THEN 'Criança' | ||
WHEN idade BETWEEN 12 AND 17 THEN 'Adolescente' | ||
WHEN idade BETWEEN 18 AND 24 THEN 'Jovem Adulto' | ||
WHEN idade BETWEEN 25 AND 39 THEN 'Adulto Jovem' | ||
WHEN idade BETWEEN 40 AND 59 THEN 'Adulto' | ||
ELSE 'Idoso' | ||
END AS idade | ||
FROM | ||
raw_table; |
29 changes: 29 additions & 0 deletions
29
queries/models/protecao_social_cadunico_hashed/identificacao_primeira_pessoa.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,29 @@ | ||
CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.identificacao_primeira_pessoa` AS | ||
with raw_table AS | ||
( | ||
SELECT | ||
-- id_pessoa, | ||
-- id_original_pessoa, | ||
id_familia, | ||
id_membro_familia, | ||
id_raca_cor, | ||
id_sexo, | ||
DATE_DIFF(CURRENT_DATE, CAST(data_nascimento AS DATE), YEAR) idade, | ||
trabalho_infantil, | ||
data_particao | ||
FROM `rj-smas.protecao_social_cadunico.identificacao_primeira_pessoa` | ||
WHERE data_particao = "2024-06-15" | ||
AND id_estado_cadastral != "4" | ||
) | ||
SELECT | ||
* EXCEPT(idade), | ||
CASE | ||
WHEN idade < 12 THEN 'Criança' | ||
WHEN idade BETWEEN 12 AND 17 THEN 'Adolescente' | ||
WHEN idade BETWEEN 18 AND 24 THEN 'Jovem Adulto' | ||
WHEN idade BETWEEN 25 AND 39 THEN 'Adulto Jovem' | ||
WHEN idade BETWEEN 40 AND 59 THEN 'Adulto' | ||
ELSE 'Idoso' | ||
END AS idade | ||
FROM | ||
raw_table; |
15 changes: 15 additions & 0 deletions
15
queries/models/protecao_social_cadunico_hashed/pessoa_deficiencia.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,15 @@ | ||
CREATE OR REPLACE TABLE `rj-smas-dev-432320.protecao_social_cadunico.pessoa_deficiencia` AS | ||
SELECT | ||
id_familia, | ||
id_membro_familia, | ||
tem_deficiencia, | ||
deficiencia_baixa_visao, | ||
deficiencia_cegueira, | ||
deficiencia_fisica, | ||
deficiencia_mental, | ||
deficiencia_sindrome_down, | ||
deficiencia_surdez_leve, | ||
deficiencia_surdez_profunda, | ||
deficiencia_transtorno_mental, | ||
FROM `rj-smas.protecao_social_cadunico.pessoa_deficiencia` | ||
where data_particao >= "2024-07-01" |