From 0a1c54582f11d5235cebdbfea1c6aba8c813e689 Mon Sep 17 00:00:00 2001 From: Lucas Moreira <65978482+lucasnascm@users.noreply.github.com> Date: Mon, 19 Feb 2024 07:58:33 -0300 Subject: [PATCH] [dados-atualizacao] world_wb_mides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inclui RJ, município de SP, DF e SC --- .../world_wb_mides__empenho.sql | 163 +++++++++++++----- .../world_wb_mides__liquidacao.sql | 136 +++++++++++---- .../world_wb_mides__orgao_unidade_gestora.sql | 7 +- .../world_wb_mides__pagamento.sql | 127 ++++++++++---- ...b_mides__raw_despesa_ato_rj_municipio.sql} | 0 .../world_wb_mides__raw_empenho_rj | 1 - .../world_wb_mides__raw_empenho_rj.sql | 0 .../world_wb_mides__raw_empenho_sc.sql | 0 8 files changed, 318 insertions(+), 116 deletions(-) rename models/world_wb_mides/{world_wb_mides__raw_despesa_ato_rj_municipio => world_wb_mides__raw_despesa_ato_rj_municipio.sql} (100%) delete mode 100644 models/world_wb_mides/world_wb_mides__raw_empenho_rj create mode 100644 models/world_wb_mides/world_wb_mides__raw_empenho_rj.sql create mode 100644 models/world_wb_mides/world_wb_mides__raw_empenho_sc.sql diff --git a/models/world_wb_mides/world_wb_mides__empenho.sql b/models/world_wb_mides/world_wb_mides__empenho.sql index 39320738..d509d78e 100644 --- a/models/world_wb_mides/world_wb_mides__empenho.sql +++ b/models/world_wb_mides/world_wb_mides__empenho.sql @@ -6,7 +6,7 @@ partition_by={ "field": "ano", "data_type": "int64", - "range": {"start": 1994, "end": 2022, "interval": 1}, + "range": {"start": 1994, "end": 2024, "interval": 1}, }, cluster_by=["mes", "sigla_uf"], labels={"tema": "economia"}, @@ -107,7 +107,7 @@ from safe_cast(codigo_elemento_despesa as int64) as string ) as modalidade_despesa, round(safe_cast(valor_empenhado as float64), 2) as valor_inicial, - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_ce` e + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_ce` e ), anulacao_ce as ( select @@ -129,7 +129,7 @@ from round( sum(safe_cast(valor_anulacao as float64)), 2 ) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_anulacao_ce` + from `basedosdados-dev.world_wb_mides_staging.raw_anulacao_ce` group by 1 ), frequencia_ce as ( @@ -240,7 +240,7 @@ from ), 2 ) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_mg` + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_mg` ), dlic as ( select @@ -419,15 +419,15 @@ from cd_elemento ) as elemento_despesa, safe_cast(vl_empenho as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_pb` e + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_pb` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pb` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pb` m on e.cd_ugestora = safe_cast(m.id_unidade_gestora as string) left join - `basedosdados-staging.world_wb_mides_staging.aux_funcao` f + `basedosdados-dev.world_wb_mides_staging.aux_funcao` f on e.de_funcao = f.nome_funcao left join - `basedosdados-staging.world_wb_mides_staging.aux_subfuncao` sf + `basedosdados-dev.world_wb_mides_staging.aux_subfuncao` sf on e.de_subfuncao = sf.nome_subfuncao ), anulacao_pb as ( @@ -444,9 +444,9 @@ from ) as string ) as id_empenho_bd, sum(safe_cast(vl_estorno as float64)) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_estorno_pb` a + from `basedosdados-dev.world_wb_mides_staging.raw_estorno_pb` a left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pb` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pb` m on a.cd_ugestora = safe_cast(m.id_unidade_gestora as string) group by 1 ), @@ -848,12 +848,12 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valorempenhado as float64), 2) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_pe` e + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_pe` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pe` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pe` m on e.nomeunidadegestora = m.nomeunidadegestora left join - `basedosdados-staging.world_wb_mides_staging.aux_funcao` fun + `basedosdados-dev.world_wb_mides_staging.aux_funcao` fun on upper( trim( replace( @@ -867,7 +867,7 @@ from ) = upper(nome_funcao) left join - `basedosdados-staging.world_wb_mides_staging.aux_subfuncao` sub + `basedosdados-dev.world_wb_mides_staging.aux_subfuncao` sub on upper(trim(e.subfuncao)) = upper(nome_subfuncao) ), empenho_pr as ( @@ -910,7 +910,7 @@ from - ifnull(safe_cast(vlestornoempenho as float64), 0), 2 ) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_pr` e + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_pr` e left join basedosdados.br_bd_diretorios_brasil.municipio m on e.cdibge = m.id_municipio_6 @@ -959,9 +959,9 @@ from replace(cd_elemento, '.', '') as string ) as elemento_despesa, safe_cast(vl_empenho as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -1009,9 +1009,9 @@ from ) as string ) as id_empenho_bd, -1 * sum(safe_cast(vl_empenho as float64)) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -1252,15 +1252,15 @@ from safe_cast(safe_cast(cd_acao as int64) as string) as acao, safe_cast((left(ds_elemento, 8)) as string) as elemento_despesa, safe_cast(replace(vl_despesa, ',', '.') as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp` e + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_sp` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_sp` m on m.ds_orgao = e.ds_orgao left join - `basedosdados-staging.world_wb_mides_staging.aux_funcao` + `basedosdados-dev.world_wb_mides_staging.aux_funcao` on ds_funcao_governo = upper(nome_funcao) left join - `basedosdados-staging.world_wb_mides_staging.aux_subfuncao` + `basedosdados-dev.world_wb_mides_staging.aux_subfuncao` on ds_subfuncao_governo = upper(nome_subfuncao) where tp_despesa = 'Empenhado' ), @@ -1286,9 +1286,9 @@ from sum( safe_cast(replace(vl_despesa, ',', '.') as float64) ) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp` a + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp` a left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_sp` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_sp` m on m.ds_orgao = a.ds_orgao where tp_despesa = 'Anulação' group by 1 @@ -1309,9 +1309,9 @@ from sum( safe_cast(replace(vl_despesa, ',', '.') as float64) ) as valor_reforco - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp` r + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp` r left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_sp` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_sp` m on m.ds_orgao = r.ds_orgao where tp_despesa = 'Reforço' group by 1 @@ -1537,7 +1537,7 @@ from 2 ) as valor_final, from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp_municipio` ), empenhado_municipio_rj_v1 as ( select @@ -1608,7 +1608,7 @@ from ) as modalidade_despesa, round(safe_cast(valor_empenhado as float64), 2) as valor_final, from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_rj_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_rj_municipio` where (safe_cast(exercicio_empenho as int64)) < 2017 ), frequencia_rj_v1 as ( @@ -1791,8 +1791,7 @@ from ) as string ) as elemento_despesa, round(safe_cast(valor as float64), 2) as valor_inicial, - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato = 'EMPENHO' ), anulacao_municipio_rj_v2 as ( @@ -1811,8 +1810,7 @@ from ) as string ) as id_empenho_bd, round(sum(safe_cast(valor as float64)), 2) as valor_anulacao, - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato = 'CANCELAMENTO EMPENHO' group by 1 ), @@ -1885,7 +1883,7 @@ from safe_cast(atividade as string) as acao, safe_cast(elemento_despesa as string) as elemento_despesa, round(safe_cast(valor as float64), 2) as valor_inicial, - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_rj` + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_rj` where numero_empenho is not null ), anulacao_rj as ( @@ -1902,7 +1900,7 @@ from ) as string ) as id_empenho_bd, round(safe_cast(valor as float64), 2) as valor_anulacao, - from `basedosdados-staging.world_wb_mides_staging.raw_anulacao_rj` + from `basedosdados-dev.world_wb_mides_staging.raw_anulacao_rj` where despesa_liquidada = 'NÃO' and numero_empenho is not null ), empenho_rj as ( @@ -2025,16 +2023,93 @@ from safe_cast(codigo_programa as string) as programa, safe_cast(codigo_acao as string) as acao, safe_cast(codigo_natureza as string) as elemento_despesa, - round( - safe_cast(replace(valor_inicial, ',', '.') as float64), 2 - ) as valor_inicial, + round(safe_cast(0 as float64), 2) as valor_inicial, round(safe_cast(0 as float64), 2) as valor_reforco, round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, - round( - safe_cast(replace (valor_final, ',', '.') as float64), 2 - ) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_df` + round(safe_cast(replace (valor_final, ',', '.') as float64), 2) as valor_final + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_df` + ), + empenhado_sc AS ( + select + safe_cast (ano_emp as int64) as ano, + safe_cast (substring(trim(data_empenho),-7,2) as int64) as mes, + safe_cast (concat(substring(trim(data_empenho),-4),'-',substring(trim(data_empenho),-7,2),'-',substring(trim(data_empenho),1,2))as date) as data, + 'SC' as sigla_uf, + safe_cast (id_municipio as string) as id_municipio, + safe_cast (codigo_orgao as string) as orgao, + safe_cast (null as string) as id_unidade_gestora, + safe_cast (null as string) as id_licitacao_bd, + safe_cast (case + when (split(nr_licitacao_contrato_convenio, ' / ')[offset(0)]) != "Sem Licitação" and + (split(nr_licitacao_contrato_convenio, ' / ')[offset(0)]) != "Sem licitação" and + (split(nr_licitacao_contrato_convenio, ' / ')[offset(0)]) != "Sem Licitacao" and + (split(nr_licitacao_contrato_convenio, ' / ')[offset(0)]) != "SEM LICITACAO" + then (split(nr_licitacao_contrato_convenio, ' / ')[offset(0)]) else '' + end as string) as id_licitacao, + safe_cast (null as string) as modalidade_licitacao, + safe_cast (concat(num_empenho, + ' ', + codigo_orgao, + ' ', + id_municipio, + ' ', + (right(cast (ano_emp as string),2))) + as string) as id_empenho_bd, + safe_cast (null as string) as id_empenho, + safe_cast (num_empenho as string) as numero, + safe_cast (LOWER (descricao_historico_empenho) as string) as descricao, + safe_cast (null as string) as modalidade, + safe_cast (cast(left(funcao, 2) as int64) as string) as funcao, + safe_cast (cast(left(subfuncao, 3) as int64) as string) as subfuncao, + safe_cast (null as string) as programa, + safe_cast (null as string) as acao, + safe_cast (elemento_despesa as string) as elemento_despesa, + round(safe_cast (valor_empenho as float64),2) as valor_inicial, + round(safe_cast (0 as float64),2) as valor_reforco, + round(safe_cast (0 as float64),2) as valor_anulacao, + round(safe_cast (0 as float64),2) as valor_ajuste, + round(safe_cast (valor_empenho as float64),2) as valor_final + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_sc` + ), + frequencia_sc as ( + select id_empenho_bd, count(id_empenho_bd) as frequencia_id + from empenhado_sc + group by 1 + order by 2 desc + ), + empenho_sc as ( + select + e.ano, + e.mes, + e.data, + e.sigla_uf, + e.id_municipio, + e.orgao, + e.id_unidade_gestora, + e.id_licitacao_bd, + e.id_licitacao, + e.modalidade_licitacao, + (case + when frequencia_id > 1 + then (safe_cast (null as string)) else e.id_empenho_bd + end) as id_empenho_bd, + e.id_empenho, + e.numero, + e.descricao, + e.modalidade, + e.funcao, + e.subfuncao, + e.programa, + e.acao, + e.elemento_despesa, + round(safe_cast (0 as float64),2) as valor_inicial, + round(safe_cast (0 as float64),2) as valor_reforco, + round(safe_cast (0 as float64),2) as valor_anulacao, + round(safe_cast (0 as float64),2) as valor_ajuste, + e.valor_final AS valor_final + from empenhado_sc e + left join frequencia_sc f on e.id_empenho_bd = f.id_empenho_bd ) select * @@ -2061,4 +2136,6 @@ from (select * from empenho_municipio_rj_v2) union all (select * from empenho_df) - ) + union all + (select * from empenho_sc) + ) \ No newline at end of file diff --git a/models/world_wb_mides/world_wb_mides__liquidacao.sql b/models/world_wb_mides/world_wb_mides__liquidacao.sql index 89794625..de5e046f 100644 --- a/models/world_wb_mides/world_wb_mides__liquidacao.sql +++ b/models/world_wb_mides/world_wb_mides__liquidacao.sql @@ -6,7 +6,7 @@ partition_by={ "field": "ano", "data_type": "int64", - "range": {"start": 1995, "end": 2022, "interval": 1}, + "range": {"start": 1995, "end": 2024, "interval": 1}, }, cluster_by=["mes", "sigla_uf"], labels={"tema": "economia"}, @@ -82,9 +82,9 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor_liquidado as float64), 2) as valor_final, - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_ce` l + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_ce` l left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_ce` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_ce` m on l.codigo_municipio = m.codigo_municipio ), liquidacao_mg as ( @@ -161,11 +161,9 @@ from - ifnull(safe_cast(valor_anulado as float64), 0), 2 ) as valor_final - from - `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_mg ` - as l + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_mg` as l left join - `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg ` as r + `basedosdados-dev.world_wb_mides_staging.raw_rsp_mg` as r on l.id_rsp = r.id_rsp ), liquidacao_pb as ( @@ -208,9 +206,9 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(vl_liquidacao as float64), 2) as valor_final, - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_pb` l + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_pb` l left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pb` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pb` m on l.cd_ugestora = safe_cast(m.id_unidade_gestora as string) ), liquidacao_pr as ( @@ -243,12 +241,12 @@ from ) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(vlliquidacaoliquido as float64), 2) as valor_final, - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_pr` l + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_pr` l left join basedosdados.br_bd_diretorios_brasil.municipio m on cdibge = id_municipio_6 left join - `basedosdados-staging.world_wb_mides_staging.raw_empenho_pr` e + `basedosdados-dev.world_wb_mides_staging.raw_empenho_pr` e on l.idempenho = e.idempenho ), liquidacao_pe as ( @@ -273,9 +271,9 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor as float64), 2) as valor_final, - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_pe` l + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_pe` l left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pe` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pe` m on l.id_unidade_gestora = safe_cast(m.id_unidadegestora as string) ), liquidado_rs as ( @@ -326,9 +324,9 @@ from safe_cast(null as string) as documento_responsavel, safe_cast(null as bool) as indicador_restos_pagar, safe_cast(vl_liquidacao as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -350,9 +348,9 @@ from ) as string ) as id_empenho_bd, -1 * sum(safe_cast(vl_liquidacao as float64)) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -511,15 +509,15 @@ from safe_cast(cd_acao as string) as acao, safe_cast((left(ds_elemento, 8)) as string) as elemento_despesa, safe_cast(replace(vl_despesa, ',', '.') as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp` e + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_sp` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_sp` m on m.ds_orgao = e.ds_orgao left join - `basedosdados-staging.world_wb_mides_staging.aux_funcao` + `basedosdados-dev.world_wb_mides_staging.aux_funcao` on ds_funcao_governo = upper(nome_funcao) left join - `basedosdados-staging.world_wb_mides_staging.aux_subfuncao` + `basedosdados-dev.world_wb_mides_staging.aux_subfuncao` on ds_subfuncao_governo = upper(nome_subfuncao) where tp_despesa = 'Valor Liquidado' ), @@ -686,8 +684,7 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(liquidado as float64), 2) as valor_final - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp_municipio` + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp_municipio` ), liquidado_municipio_rj_v1 as ( select @@ -725,9 +722,8 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor_liquidado as float64), 2) as valor_final - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_rj_municipio` - where (safe_cast(exercicio_empenho as int64)) < 2017 + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rj_municipio` + where (safe_cast (exercicio_empenho as int64)) < 2017 ), frequencia_rj_v1 as ( select id_empenho_bd, count(id_empenho_bd) as frequencia_id @@ -812,7 +808,7 @@ from safe_cast(null as bool) as indicador_restos_pagar, round(safe_cast(valor as float64), 2) as valor_inicial from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato = 'LIQUIDACAO' ), anulacao_municipio_rj_v2 as ( @@ -833,7 +829,7 @@ from ) as id_empenho_bd, sum(safe_cast(valor as float64)) as valor_anulacao, from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato in ( 'CANCELAMENTO LIQUIDACAO', @@ -912,7 +908,7 @@ from round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor as float64), 2) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_rj` + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_rj` where numero_empenho is not null ), liquidacao_df as ( @@ -961,16 +957,80 @@ from safe_cast(credor as string) as nome_responsavel, safe_cast(cnpj_cpf_credor as string) as documento_responsavel, safe_cast(null as bool) as indicador_restos_pagar, - round( - safe_cast(replace (valor, ',', '.') as float64), 2 - ) as valor_inicial, + round(safe_cast(0 as float64), 2) as valor_inicial, round(safe_cast(0 as float64), 2) as valor_anulacao, round(safe_cast(0 as float64), 2) as valor_ajuste, round( - safe_cast(replace (valor, ',', '.') as float64), 2 - ) as valor_final - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_df` - ) + safe_cast(replace(valor, ',', '.') as float64), 2 + ) as valor_inicial, + from `basedosdados-dev.world_wb_mides_staging.raw_liquidacao_df` + ), + liquidado_sc as ( + select + safe_cast (ano_emp as int64) as ano, + safe_cast (substring(trim(data_empenho),-7,2) as int64) as mes, + safe_cast (null as date) as data, + 'SC' as sigla_uf, + safe_cast (id_municipio as string) as id_municipio, + safe_cast (codigo_orgao as string) as orgao, + safe_cast (null as string) as id_unidade_gestora, + safe_cast (concat( + num_empenho, + ' ', + codigo_orgao, + ' ', + id_municipio, + ' ', + (right(cast(ano_emp as string),2))) as string + ) as id_empenho_bd, + safe_cast (null as string) as id_empenho, + safe_cast (num_empenho as string) as numero_empenho, + safe_cast (null as string) as id_liquidacao_bd, + safe_cast (null as string) as id_liquidacao, + safe_cast (null as string) as numero, + safe_cast (null as string) as nome_responsavel, + safe_cast (null as string) as documento_responsavel, + safe_cast (null as bool) as indicador_restos_pagar, + round(safe_cast (0 as float64),2) as valor_inicial, + round(safe_cast (0 as float64),2) as valor_reforco, + round(safe_cast (0 as float64),2) as valor_anulacao, + round(safe_cast (0 as float64),2) as valor_ajuste, + round(safe_cast (valor_liquidacao as float64),2) as valor_final + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_sc` + ), + frequencia_sc as ( + select id_empenho_bd, count(id_empenho_bd) as frequencia_id + from liquidado_sc + group by 1 + order by 2 desc + ), + liquidacao_sc as ( + select + l.ano, + l.mes, + l.data, + l.sigla_uf, + l.id_municipio, + l.orgao, + l.id_unidade_gestora, + (case when frequencia_id > 1 then (safe_cast (null as string)) else l.id_empenho_bd end) as id_empenho_bd, + l.id_empenho, + l.numero_empenho, + l.id_liquidacao_bd, + l.id_liquidacao, + l.numero, + l.nome_responsavel, + l.documento_responsavel, + l.indicador_restos_pagar, + l.valor_inicial, + l.valor_anulacao, + l.valor_ajuste, + l.valor_final + from liquidado_sc l + left join frequencia_sc f on l.id_empenho_bd = f.id_empenho_bd + ) + + select * from liquidacao_mg @@ -996,4 +1056,6 @@ from (select * from liquidacao_rj) union all (select * from liquidacao_df) - ) + union all + (select * from liquidacao_sc) + ) \ No newline at end of file diff --git a/models/world_wb_mides/world_wb_mides__orgao_unidade_gestora.sql b/models/world_wb_mides/world_wb_mides__orgao_unidade_gestora.sql index 886c1309..536a46d7 100644 --- a/models/world_wb_mides/world_wb_mides__orgao_unidade_gestora.sql +++ b/models/world_wb_mides/world_wb_mides__orgao_unidade_gestora.sql @@ -4,10 +4,9 @@ schema="world_wb_mides", materialized="table", cluster_by=["sigla_uf"], - labels={"tema": "economia"}, + labels={"project_id": "basedosdados", "tema": "economia"}, ) }} --- inclui novos municípios e estados select safe_cast(ano as string) ano, safe_cast(sigla_uf as string) sigla_uf, @@ -17,4 +16,6 @@ select safe_cast(id_unidade_gestora as string) id_unidade_gestora, safe_cast(nome_unidade_gestora as string) nome_unidade_gestora, safe_cast(esfera as string) esfera -from `basedosdados-staging.world_wb_mides_staging.orgao_unidade_gestora` as t +from `basedosdados-dev.world_wb_mides_staging.orgao_unidade_gestora` as t + +-- contém 10 UFs: CE, DF, MG, PB, PE, PR, RJ, RS, SC, SP diff --git a/models/world_wb_mides/world_wb_mides__pagamento.sql b/models/world_wb_mides/world_wb_mides__pagamento.sql index 0ef7f322..7621520d 100644 --- a/models/world_wb_mides/world_wb_mides__pagamento.sql +++ b/models/world_wb_mides/world_wb_mides__pagamento.sql @@ -65,9 +65,9 @@ from ) as string ) as documento_credor, safe_cast(safe_cast(codigo_fonte_ as int64) as string) as fonte, - from `basedosdados-staging.world_wb_mides_staging.raw_empenho_ce` e + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_ce` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_ce` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_ce` m on e.codigo_municipio = m.codigo_municipio ), pago_ce as ( @@ -136,9 +136,9 @@ from round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor_nota_pagamento as float64), 2) as valor_final, round(safe_cast(0 as float64), 2) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_pagamento_ce` p + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_ce` p left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_ce` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_ce` m on p.codigo_municipio = m.codigo_municipio ), frequencia_ce as ( @@ -296,10 +296,9 @@ from - ifnull(safe_cast(vlr_ret_fonte as float64), 0), 2 ) as valor_liquido_recebido, - from - `basedosdados-staging.world_wb_mides_staging.raw_pagamento_mg ` as p + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_mg` as p left join - `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg ` as r + `basedosdados-dev.world_wb_mides_staging.raw_rsp_mg` as r on p.id_rsp = r.id_rsp ), pago_pb as ( @@ -365,15 +364,15 @@ from - safe_cast(vl_retencao as float64), 2 ) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_pagamento_pb` p + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_pb` p left join - `basedosdados-staging.world_wb_mides_staging.raw_empenho_pb` e + `basedosdados-dev.world_wb_mides_staging.raw_empenho_pb` e on p.nu_empenho = e.nu_empenho and p.cd_ugestora = e.cd_ugestora and p.de_uorcamentaria = e.de_uorcamentaria and p.dt_ano = e.dt_ano left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pb` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pb` m on safe_cast(e.cd_ugestora as string) = safe_cast(m.id_unidade_gestora as string) ), @@ -456,9 +455,9 @@ from 2 ) as valor_final, round(safe_cast(0 as float64), 2) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_pagamento_pe` p + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_pe` p inner join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_pe` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_pe` m on safe_cast(p.id_unidade_gestora as string) = safe_cast(m.id_unidadegestora as string) ), @@ -497,9 +496,9 @@ from round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(p.cdibge as float64), 2) as valor_final, round(safe_cast(0 as float64), 2) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_pagamento_pr` p + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_pr` p left join - `basedosdados-staging.world_wb_mides_staging.raw_empenho_pr` e + `basedosdados-dev.world_wb_mides_staging.raw_empenho_pr` e on p.idempenho = e.idempenho left join basedosdados.br_bd_diretorios_brasil.municipio m @@ -573,9 +572,9 @@ from safe_cast(null as bool) as indicador_restos_pagar, safe_cast(null as string) as fonte, safe_cast(vl_pagamento as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -618,9 +617,9 @@ from ) as string ) as id_empenho_bd, -1 * sum(safe_cast(vl_pagamento as float64)) as valor_anulacao - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_rs` as c + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rs` as c left join - `basedosdados-staging.world_wb_mides_staging.aux_orgao_rs` as a + `basedosdados-dev.world_wb_mides_staging.aux_orgao_rs` as a on c.cd_orgao = a.cd_orgao left join `basedosdados.br_bd_diretorios_brasil.municipio` m @@ -878,15 +877,15 @@ from safe_cast(cd_acao as string) as acao, safe_cast((left(ds_elemento, 8)) as string) as elemento_despesa, safe_cast(replace(vl_despesa, ',', '.') as float64) as valor_inicial - from `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp` e + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp` e left join - `basedosdados-staging.world_wb_mides_staging.aux_municipio_sp` m + `basedosdados-dev.world_wb_mides_staging.aux_municipio_sp` m on m.ds_orgao = e.ds_orgao left join - `basedosdados-staging.world_wb_mides_staging.aux_funcao` + `basedosdados-dev.world_wb_mides_staging.aux_funcao` on ds_funcao_governo = upper(nome_funcao) left join - `basedosdados-staging.world_wb_mides_staging.aux_subfuncao` + `basedosdados-dev.world_wb_mides_staging.aux_subfuncao` on ds_subfuncao_governo = upper(nome_subfuncao) where tp_despesa = 'Valor Pago' ), @@ -1124,8 +1123,7 @@ from round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(pago as float64), 2) as valor_final, round(safe_cast(pago as float64), 2) as valor_liquido_recebido, - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp_municipio` + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_sp_municipio` ), pago_municipio_rj_v1 as ( select @@ -1168,9 +1166,7 @@ from round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor_pago as float64), 2) as valor_final, round(safe_cast(valor_pago as float64), 2) as valor_liquido_recebido - from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_rj_municipio` - where (safe_cast(exercicio_empenho as int64)) < 2017 + from `basedosdados-dev.world_wb_mides_staging.raw_despesa_rj_municipio` ), frequencia_rj_v1 as ( select id_empenho_bd, count(id_empenho_bd) as frequencia_id @@ -1278,7 +1274,7 @@ from safe_cast(fonterecursos as string) as fonte, round(safe_cast(valor as float64), 2) as valor_inicial, from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato = 'PAGAMENTO' ), anulacao_municipio_rj_v2 as ( @@ -1299,7 +1295,7 @@ from ) as id_empenho_bd, sum(safe_cast(valor as float64)) as valor_anulacao, from - `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + `basedosdados-dev.world_wb_mides_staging.raw_despesa_ato_rj_municipio` where tipoato in ( 'CANCEL.PAGAMENTO RET.DOTAÇÃO', @@ -1394,7 +1390,7 @@ from round(safe_cast(0 as float64), 2) as valor_ajuste, round(safe_cast(valor as float64), 2) as valor_final, round(safe_cast(valor as float64), 2) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_rj` + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_rj` where numero_empenho is not null ), pagamento_df as ( @@ -1493,7 +1489,72 @@ from - safe_cast(replace(valor_cancelado, ',', '.') as float64), 2 ) as valor_liquido_recebido, - from `basedosdados-staging.world_wb_mides_staging.raw_pagamento_df` + from `basedosdados-dev.world_wb_mides_staging.raw_pagamento_df` + ), + pago_sc AS ( + select + safe_cast (ano_emp as int64) as ano, + safe_cast (substring(trim(data_empenho),-7,2) as int64) as mes, + safe_cast (null as date) as data, + 'SC' as sigla_uf, + safe_cast (id_municipio as string) as id_municipio, + safe_cast (codigo_orgao as string) as orgao, + safe_cast (null as string) as id_unidade_gestora, + safe_cast (concat(num_empenho, ' ', codigo_orgao, ' ', id_municipio, ' ', (right(cast(ano_emp as string),2))) as string) as id_empenho_bd, + safe_cast (null as string) as id_empenho, + safe_cast (num_empenho as string) as numero_empenho, + safe_cast (null as string) as id_liquidacao_bd, + safe_cast (null as string) as id_liquidacao, + safe_cast (null as string) as numero_liquidacao, + safe_cast (null as string) as id_pagamento_bd, + safe_cast (null as string) as id_pagamento, + safe_cast (null as string) as numero, + safe_cast (nome_credor as string) as nome_credor, + safe_cast (cpf_cnpj as string) as documento_credor, + safe_cast (null as bool) as indicador_restos_pagar, + safe_cast (right(especificacao_fonte_recurso,2) as string) as fonte, + round(safe_cast (0 as float64),2) as valor_inicial, + round(safe_cast (0 as float64),2) as valor_anulacao, + round(safe_cast (0 as float64),2) as valor_ajuste, + round(safe_cast (valor_pagamento as float64),2) as valor_final, + round(safe_cast (valor_pagamento as float64),2) as valor_liquido_recebido + from `basedosdados-dev.world_wb_mides_staging.raw_empenho_sc` + ), + frequencia_sc as ( + select id_empenho_bd, count(id_empenho_bd) as frequencia_id + from pago_sc + group by 1 + order by 2 desc + ), + pagamento_sc as ( + select + p.ano, + p.mes, + p.data, + p.sigla_uf, + p.id_municipio, + p.orgao, + p.id_unidade_gestora, + (case when frequencia_id > 1 then (safe_cast (null as string)) else p.id_empenho_bd end) as id_empenho_bd, + p.id_empenho, + p.numero_empenho, + p.id_liquidacao_bd, + p.id_liquidacao, + p.numero_liquidacao, + p.id_pagamento_bd, + p.id_pagamento, + p.numero, + p.nome_credor, + p.documento_credor, + p.indicador_restos_pagar, + p.fonte, + p.valor_inicial, + p.valor_anulacao, + p.valor_ajuste, + p.valor_final, + p.valor_liquido_recebido + from pago_sc p + left join frequencia_sc f on p.id_empenho_bd = f.id_empenho_bd ) select * @@ -1520,4 +1581,6 @@ from (select * from pagamento_rj) union all (select * from pagamento_df) - ) + union all + (select * from pagamento_sc) + ) \ No newline at end of file diff --git a/models/world_wb_mides/world_wb_mides__raw_despesa_ato_rj_municipio b/models/world_wb_mides/world_wb_mides__raw_despesa_ato_rj_municipio.sql similarity index 100% rename from models/world_wb_mides/world_wb_mides__raw_despesa_ato_rj_municipio rename to models/world_wb_mides/world_wb_mides__raw_despesa_ato_rj_municipio.sql diff --git a/models/world_wb_mides/world_wb_mides__raw_empenho_rj b/models/world_wb_mides/world_wb_mides__raw_empenho_rj deleted file mode 100644 index 4ba28051..00000000 --- a/models/world_wb_mides/world_wb_mides__raw_empenho_rj +++ /dev/null @@ -1 +0,0 @@ --- diff --git a/models/world_wb_mides/world_wb_mides__raw_empenho_rj.sql b/models/world_wb_mides/world_wb_mides__raw_empenho_rj.sql new file mode 100644 index 00000000..e69de29b diff --git a/models/world_wb_mides/world_wb_mides__raw_empenho_sc.sql b/models/world_wb_mides/world_wb_mides__raw_empenho_sc.sql new file mode 100644 index 00000000..e69de29b