diff --git a/models/world_wb_mides/world_wb_mides__empenho.sql b/models/world_wb_mides/world_wb_mides__empenho.sql index 39320738..966f7dce 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"}, @@ -2025,9 +2025,7 @@ 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, @@ -2035,6 +2033,132 @@ from safe_cast(replace (valor_final, ',', '.') as float64), 2 ) as valor_final from `basedosdados-staging.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-staging.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 +2185,6 @@ from (select * from empenho_municipio_rj_v2) union all (select * from empenho_df) + union all + (select * from empenho_sc) ) diff --git a/models/world_wb_mides/world_wb_mides__liquidacao.sql b/models/world_wb_mides/world_wb_mides__liquidacao.sql index 89794625..10b080a1 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"}, @@ -162,10 +162,9 @@ from 2 ) as valor_final from - `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_mg ` - as l + `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_mg` as l left join - `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg ` as r + `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg` as r on l.id_rsp = r.id_rsp ), liquidacao_pb as ( @@ -961,15 +960,85 @@ 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 + safe_cast(replace(valor, ',', '.') as float64), 2 + ) as valor_inicial, from `basedosdados-staging.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-staging.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 * @@ -996,4 +1065,6 @@ from (select * from liquidacao_rj) union all (select * from liquidacao_df) + union all + (select * from liquidacao_sc) ) 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..188db1e8 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,8 @@ 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-staging.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..c3ea51eb 100644 --- a/models/world_wb_mides/world_wb_mides__pagamento.sql +++ b/models/world_wb_mides/world_wb_mides__pagamento.sql @@ -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-staging.world_wb_mides_staging.raw_pagamento_mg` as p left join - `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg ` as r + `basedosdados-staging.world_wb_mides_staging.raw_rsp_mg` as r on p.id_rsp = r.id_rsp ), pago_pb as ( @@ -1170,7 +1169,6 @@ from 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 ), frequencia_rj_v1 as ( select id_empenho_bd, count(id_empenho_bd) as frequencia_id @@ -1394,7 +1392,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-staging.world_wb_mides_staging.raw_pagamento_rj` where numero_empenho is not null ), pagamento_df as ( @@ -1494,6 +1492,89 @@ from 2 ) as valor_liquido_recebido, from `basedosdados-staging.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-staging.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 +1601,6 @@ from (select * from pagamento_rj) union all (select * from pagamento_df) + union all + (select * from pagamento_sc) ) 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 75% 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 index 4ba28051..884a3f60 100644 --- 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 @@ -1 +1,2 @@ -- + 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