From e251d955b240f5a76483c3f3783127f0b3c3af09 Mon Sep 17 00:00:00 2001 From: Lucas Moreira <65978482+lucasnascm@users.noreply.github.com> Date: Fri, 22 Dec 2023 16:28:17 -0300 Subject: [PATCH] [dados-atualizacao] world_wb_mides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add novos municípios e estados na base via incrementos nas queries --- .../world_wb_mides__empenho.sql | 343 +++++++++++++++++- .../world_wb_mides__liquidacao.sql | 202 ++++++++++- .../world_wb_mides__pagamento.sql | 236 +++++++++++- 3 files changed, 776 insertions(+), 5 deletions(-) diff --git a/models/world_wb_mides/world_wb_mides__empenho.sql b/models/world_wb_mides/world_wb_mides__empenho.sql index 2cf3bdaf..0e5ad894 100644 --- a/models/world_wb_mides/world_wb_mides__empenho.sql +++ b/models/world_wb_mides/world_wb_mides__empenho.sql @@ -11,7 +11,7 @@ "end": 2022, "interval": 1} }, - cluster_by = ["ano", "sigla_uf"], + cluster_by = ["mes", "sigla_uf"], labels = {'tema': 'economia'}) }} SELECT @@ -52,7 +52,12 @@ WITH empenhado_ce AS ( SAFE_CAST (TRIM(codigo_unidade) AS STRING) AS id_unidade_gestora, SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, SAFE_CAST (numero_licitacao AS STRING) AS id_licitacao, - SAFE_CAST (tipo_processo_licitatorio AS STRING) AS modalidade_licitacao, + CASE WHEN tipo_processo_licitatorio = 'N' THEN '98' + WHEN tipo_processo_licitatorio = 'R' THEN '2' + WHEN tipo_processo_licitatorio = 'D' THEN '8' + WHEN tipo_processo_licitatorio = 'I' THEN '10' + WHEN tipo_processo_licitatorio = 'R' THEN '29' + END AS modalidade_licitacao, SAFE_CAST (CONCAT(numero_empenho, ' ', TRIM(codigo_orgao), ' ', TRIM(codigo_unidade), ' ', geoibgeId, ' ', (SUBSTRING(data_emissao_empenho,6,2)), ' ', (SUBSTRING(data_emissao_empenho,3,2))) AS STRING) AS id_empenho_bd, SAFE_CAST (NULL AS STRING) AS id_empenho, SAFE_CAST (numero_empenho AS STRING) AS numero, @@ -815,15 +820,347 @@ empenhado_mg AS ( FROM empenho_completo_sp e LEFT JOIN dummies_sp d ON d.id_empenho_bd=e.id_empenho_bd GROUP BY 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 +), + empenho_municipio_sp AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data_empenho)) AS INT64)) AS mes, + SAFE_CAST (data_empenho AS DATE) AS data, + 'SP' AS sigla_uf, + '3550308' AS id_municipio, + SAFE_CAST (codigo_orgao AS STRING) AS orgao, + SAFE_CAST (codigo_unidade AS STRING) AS id_unidade_gestora, + SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, + SAFE_CAST (NULL AS STRING) AS id_licitacao, + SAFE_CAST (NULL AS STRING) AS modalidade_licitacao, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(codigo_orgao), ' ', TRIM(codigo_unidade), ' ', '3550308', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (id_empenho AS STRING) AS id_empenho, + SAFE_CAST (nr_empenho AS STRING) AS numero, + SAFE_CAST (observacoes AS STRING) AS descricao, + SAFE_CAST (LEFT(REPLACE(tipo_empenho, 'Por Estimativa', 'Estimativo'), 1) AS STRING) AS modalidade, + SAFE_CAST (codigo_funcao AS STRING) AS funcao, + SAFE_CAST (codigo_subfuncao AS STRING) AS subfuncao, + SAFE_CAST (codigo_programa_governo AS STRING) AS programa, + SAFE_CAST (codigo_projeto_atividade AS STRING) AS acao, + SAFE_CAST (codigo_conta_despesa AS STRING) AS modalidade_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 (cancelado AS FLOAT64),2) AS valor_anulacao, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_ajuste, + ROUND(SAFE_CAST (valor_empenho AS FLOAT64) - SAFE_CAST (cancelado AS FLOAT64),2) AS valor_final, + FROM `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp_municipio` +), + empenhado_municipio_rj_v1 AS ( + SELECT + (SAFE_CAST(exercicio_empenho AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data_empenho)) AS INT64)) AS mes, + SAFE_CAST (data_empenho AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (orgao_programa_trabalho AS STRING) AS orgao, + SAFE_CAST (unidade_programa_trabalho AS STRING) AS id_unidade_gestora, + SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, + SAFE_CAST (N_mero_licita__o AS STRING) AS id_licitacao, + CASE WHEN modalidade_licitacao = 'Convite' THEN '1' + WHEN modalidade_licitacao = 'Tomada De Preços' THEN '2' + WHEN modalidade_licitacao = 'Tomada de Preços' THEN '2' + WHEN modalidade_licitacao = 'Concorrência' THEN '3' + WHEN modalidade_licitacao = 'Pregão' THEN '4' + WHEN modalidade_licitacao = 'Leilão' THEN '7' + WHEN modalidade_licitacao = 'Dispensa' THEN '8' + WHEN modalidade_licitacao = 'Inexigibilidade' THEN '10' + WHEN modalidade_licitacao = 'Concurso' THEN '11' + WHEN modalidade_licitacao = 'Seleção Pública' THEN '31' + WHEN modalidade_licitacao = 'Não Sujeito' THEN '99' + END AS modalidade_licitacao, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(orgao_programa_trabalho), ' ', TRIM(unidade_programa_trabalho), ' ', '3304557', ' ', (RIGHT(exercicio_empenho,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nr_empenho AS STRING) AS numero, + SAFE_CAST (NULL AS STRING) AS descricao, + SAFE_CAST (LEFT(especie, 1) AS STRING) AS modalidade, + SAFE_CAST (CAST (SUBSTRING (programa_trabalho, 7,2) AS INT64) AS STRING) AS funcao, + SAFE_CAST (CAST (SUBSTRING (programa_trabalho, 10,3) AS INT64) AS STRING) AS subfuncao, + SAFE_CAST (SUBSTRING (programa_trabalho, 14,4) AS STRING) AS programa, + SAFE_CAST (SUBSTRING (programa_trabalho, 19,4) AS STRING) AS acao, + SAFE_CAST (SAFE_CAST (natureza_despesa AS INT64) AS STRING) 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` +), + frequencia_rj_v1 AS ( + SELECT id_empenho_bd, COUNT(id_empenho_bd) AS frequencia_id + FROM empenhado_municipio_rj_v1 + GROUP BY 1 + ORDER BY 2 DESC +), + empenho_municipio_rj_v1 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.modalidade_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_municipio_rj_v1 e + LEFT JOIN frequencia_rj_v1 f ON e.id_empenho_bd = f.id_empenho_bd +), + empenhado_municipio_rj_v2 AS ( + SELECT + (SAFE_CAST(Exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (Data)) AS INT64)) AS mes, + SAFE_CAST (Data AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (UG AS STRING) AS orgao, + SAFE_CAST (UO AS STRING) AS id_unidade_gestora, + SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, + SAFE_CAST (NULL AS STRING) AS id_licitacao, + CASE WHEN Licitacao = 'CONVITE' THEN '1' + WHEN Licitacao = 'TOMADA DE PREÇOS' THEN '2' + WHEN Licitacao = 'CONCORRÊNCIA' THEN '3' + WHEN Licitacao = 'PREGÃO' THEN '4' + WHEN Licitacao = 'PREÇO REGISTRADO/PREGÃO' THEN '4' + WHEN Licitacao = 'REGISTRO DE PREÇOS EXTERNO/PREGÃO' THEN '4' + WHEN Licitacao = 'DISPENSA' THEN '8' + WHEN Licitacao = 'INEXIGIBILIDADE' THEN '10' + WHEN Licitacao = 'CONCURSO' THEN '11' + WHEN Licitacao = 'SELEÇÃO PÚBLICA' THEN '31' + WHEN Licitacao = 'NÃO SUJEITO' THEN '99' + END AS modalidade_licitacao, + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (EmpenhoExercicio AS STRING) AS numero, + SAFE_CAST (Historico AS STRING) AS descricao, + SAFE_CAST (NULL AS STRING) AS modalidade, + SAFE_CAST (CAST (Funcao AS INT64) AS STRING) AS funcao, + SAFE_CAST (SubFuncao AS STRING) AS subfuncao, + SAFE_CAST (Programa AS STRING) AS programa, + SAFE_CAST (Acao AS STRING) AS acao, + SAFE_CAST (CONCAT ( + -- categoria econômica + CASE + WHEN Grupo = 'PESSOAL E ENCARGOS SOCIAIS' THEN '3' + WHEN Grupo = 'JUROS E ENCARGOS DA DIVIDA' THEN '3' + WHEN Grupo = 'OUTRAS DESPESAS CORRENTES' THEN '3' + WHEN Grupo = 'INVESTIMENTOS' THEN '4' + WHEN Grupo = 'INVERSOES FINANCEIRAS' THEN '4' + WHEN Grupo = 'AMORTIZACAO DA DIVIDA' THEN '4' + END, + -- natureza da despesa + CASE + WHEN Grupo = 'PESSOAL E ENCARGOS SOCIAIS' THEN '1' + WHEN Grupo = 'JUROS E ENCARGOS DA DIVIDA' THEN '2' + WHEN Grupo = 'OUTRAS DESPESAS CORRENTES' THEN '3' + WHEN Grupo = 'INVESTIMENTOS' THEN '4' + WHEN Grupo = 'INVERSOES FINANCEIRAS' THEN '5' + WHEN Grupo = 'AMORTIZACAO DA DIVIDA' THEN '6' + END, + -- modalidade de aplicação + CASE + WHEN Modalidade = 'TRANSFERENCIAS A UNIAO' THEN '20' + WHEN Modalidade = 'TRANSFERENCIAS A ESTADOS E AO DISTRITO FEDERAL' THEN '30' + WHEN Modalidade = 'TRANSFERENCIAS A INSTITUICOES PRIVADAS SEM FINS LUCRATIVOS' THEN '50' + WHEN Modalidade = 'TRANSFERENCIAS A INSTITUICOES PRIVADAS COM FINS LUCRATIVOS' THEN '60' + WHEN Modalidade = 'EXECUCAO DE CONTRATO DE PARCERIA PUBLICO-PRIVADA' THEN '67' + WHEN Modalidade = 'EXECUCAO DE CONTRATO DE PARCERIA PUBLICO-PRIVADA - PPP' THEN '67' + WHEN Modalidade = 'EXECUCAO DE CONTRATO DE PARCERIA PUBLICO PRIVADA - PPP' THEN '67' + WHEN Modalidade = 'DESP. DECORRENTES DA PART. EM FUNDOS, ORGANISMOS OU ENTIDADES ASSEMELHADAS NAC. E INTERN.' THEN '84' + WHEN Modalidade = 'APLICACOES DIRETAS' THEN '90' + WHEN Modalidade = 'APLIC. DIRETA DECOR. DE OPER. ENTRE ORG., FUNDOS E ENTID. INTEG. DO ORC. FISC. E SEG. SOC.' THEN '91' + WHEN Modalidade = 'APLIC DIRETAS DECOR DE OPER ENTRE ORG, FUNDOS E ENTID INTEGRANTES DOS ORC FISC E SEG SOC' THEN '91' + ELSE NULL + END, + -- elemento e item da despesa + Elemento, Subelemento) 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` + WHERE TipoAto = 'EMPENHO' + ), + anulacao_municipio_rj_v2 AS ( + SELECT + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_empenho_bd, + ROUND(SAFE_CAST (Valor AS FLOAT64),2) AS valor_anulacao, + FROM `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + WHERE TipoAto = 'CANCELAMENTO EMPENHO' +), + empenho_municipio_rj_v2 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, + e.id_empenho_bd, + e.id_empenho, + e.numero, + e.descricao, + e.modalidade, + e.funcao, + e.subfuncao, + e.programa, + e.acao, + e.elemento_despesa, + e.valor_inicial AS valor_inicial, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_reforco, + SAFE_CAST (IFNULL(a.valor_anulacao,0) AS FLOAT64) AS valor_anulacao, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_ajuste, + ROUND (SAFE_CAST((e.valor_inicial - IFNULL (a.valor_anulacao,0)) AS FLOAT64), 2) AS valor_final + FROM empenhado_municipio_rj_v2 e + LEFT JOIN anulacao_municipio_rj_v2 a ON e.id_empenho_bd = a.id_empenho_bd +), + empenhado_rj AS ( + SELECT + (SAFE_CAST(ano AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data)) AS INT64)) AS mes, + SAFE_CAST (data AS DATE) AS data, + 'RJ' AS sigla_uf, + SAFE_CAST (id_municipio AS STRING) AS id_municipio, + SAFE_CAST (id_orgao AS STRING) AS orgao, + SAFE_CAST (unidade_administrativa AS STRING) AS id_unidade_gestora, + SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, + SAFE_CAST (NULL AS STRING) AS id_licitacao, + SAFE_CAST (NULL AS STRING) AS modalidade_licitacao, + SAFE_CAST (CONCAT(numero_empenho, ' ', id_orgao, ' ', id_municipio, ' ', (RIGHT(ano,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (numero_empenho AS STRING) AS numero, + SAFE_CAST (descricao AS STRING) AS descricao, + SAFE_CAST (modalidade AS STRING) AS modalidade, + SAFE_CAST (CAST (funcao AS INT64) AS STRING) AS funcao, + SAFE_CAST (subfuncao AS STRING) AS subfuncao, + SAFE_CAST (programa AS STRING) AS programa, + 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` + WHERE numero_empenho IS NOT NULL +), + anulacao_rj AS ( + SELECT + SAFE_CAST (CONCAT(numero_empenho, ' ', id_orgao, ' ', id_municipio, ' ', (RIGHT(ano,2))) 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` + WHERE despesa_liquidada = 'NÃO' AND numero_empenho IS NOT NULL +), + empenho_rj 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, + e.id_empenho_bd, + e.id_empenho, + e.numero, + e.descricao, + e.modalidade, + e.funcao, + e.subfuncao, + e.programa, + e.acao, + e.elemento_despesa, + e.valor_inicial AS valor_inicial, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_reforco, + SAFE_CAST (IFNULL(a.valor_anulacao,0) AS FLOAT64) AS valor_anulacao, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_ajuste, + ROUND (SAFE_CAST((e.valor_inicial - IFNULL (a.valor_anulacao,0)) AS FLOAT64), 2) AS valor_final + FROM empenhado_rj e + LEFT JOIN anulacao_rj a ON e.id_empenho_bd = a.id_empenho_bd +), + empenho_df AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (lancamento)) AS INT64)) AS mes, + SAFE_CAST (lancamento AS DATE) AS data, + 'DF' AS sigla_uf, + '5300108' AS id_municipio, + SAFE_CAST (codigo_ug AS STRING) AS orgao, + SAFE_CAST (codigo_gestao AS STRING) AS id_unidade_gestora, + SAFE_CAST (NULL AS STRING) AS id_licitacao_bd, + SAFE_CAST (NULL AS STRING) AS id_licitacao, + CASE WHEN codigo_licitacao = '1' THEN '11' + WHEN codigo_licitacao = '2' THEN '1' + WHEN codigo_licitacao = '3' THEN '2' + WHEN codigo_licitacao = '4' THEN '3' + WHEN codigo_licitacao = '5' THEN '8' + WHEN codigo_licitacao = '6' THEN '10' + WHEN codigo_licitacao = '7' THEN '99' + WHEN codigo_licitacao = '8' THEN '32' + WHEN codigo_licitacao = '9' THEN '4' + WHEN codigo_licitacao = '10' THEN '32' + WHEN codigo_licitacao = '11' THEN '31' + WHEN codigo_licitacao = '12' THEN '' + WHEN codigo_licitacao = '13' THEN '5' + WHEN codigo_licitacao = '14' THEN '6' + WHEN codigo_licitacao = '15' THEN '5' + WHEN codigo_licitacao = '16' THEN '5' + WHEN codigo_licitacao = '17' THEN '6' + WHEN codigo_licitacao = '18' THEN '3' + WHEN codigo_licitacao = '19' THEN '32' + WHEN codigo_licitacao = '20' THEN '31' + WHEN codigo_licitacao = '21' THEN '31' + WHEN codigo_licitacao = '22' THEN '32' + WHEN codigo_licitacao = '23' THEN '12' + WHEN codigo_licitacao = '25' THEN '98' + WHEN codigo_licitacao = 'INEXIGÍVEL' THEN '10' + END AS modalidade_licitacao, + SAFE_CAST (CONCAT(RIGHT(nota_empenho, LENGTH(nota_empenho) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nota_empenho AS STRING) AS numero, + SAFE_CAST (descricao AS STRING) AS descricao, + SAFE_CAST (LEFT(modalidade_empenho, 1) AS STRING) AS modalidade, + SAFE_CAST (CAST (codigo_funcao AS INT64) AS STRING) AS funcao, + SAFE_CAST (codigo_subfuncao AS STRING) AS subfuncao, + 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 (valor_inicial 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_final AS FLOAT64),2) AS valor_final + FROM `basedosdados-staging.world_wb_mides_staging.raw_empenho_df` ) SELECT * FROM empenho_mg UNION ALL (SELECT * FROM empenho_sp) +UNION ALL (SELECT * FROM empenho_municipio_sp) UNION ALL (SELECT * FROM empenho_pe) UNION ALL (SELECT * FROM empenho_pr) UNION ALL (SELECT * FROM empenho_rs) UNION ALL (SELECT * FROM empenho_pb) UNION ALL (SELECT * FROM empenho_ce) -) +UNION ALL (SELECT * FROM empenho_rj) +UNION ALL (SELECT * FROM empenho_municipio_rj_v1) +UNION ALL (SELECT * FROM empenho_municipio_rj_v2) +UNION ALL (SELECT * FROM empenho_df) +) \ 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 7cab1745..75e1da27 100644 --- a/models/world_wb_mides/world_wb_mides__liquidacao.sql +++ b/models/world_wb_mides/world_wb_mides__liquidacao.sql @@ -419,15 +419,215 @@ WITH liquidacao_ce AS ( FROM liquidado_sp l LEFT JOIN dummies d ON d.id_empenho_bd=l.id_empenho_bd GROUP BY 4,5,6,7,8,9,10,11,12,13,14,15,16 +), + liquidacao_municipio_sp AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data_empenho)) AS INT64)) AS mes, + SAFE_CAST (data_empenho AS DATE) AS data, + 'SP' AS sigla_uf, + '3550308' AS id_municipio, + SAFE_CAST (codigo_orgao AS STRING) AS orgao, + SAFE_CAST (codigo_unidade AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(codigo_orgao), ' ', TRIM(codigo_unidade), ' ', '3550308', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (id_empenho AS STRING) AS id_empenho, + SAFE_CAST (nr_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 (liquidado 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 (liquidado AS FLOAT64),2) AS valor_final + FROM `basedosdados-staging.world_wb_mides_staging.raw_despesa_sp_municipio` +), + liquidado_municipio_rj_v1 AS ( + SELECT + SAFE_CAST(exercicio_empenho AS INT64) AS ano, + SAFE_CAST(NULL AS INT64) AS mes, + SAFE_CAST (NULL AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (orgao_programa_trabalho AS STRING) AS orgao, + SAFE_CAST (unidade_programa_trabalho AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(orgao_programa_trabalho), ' ', TRIM(unidade_programa_trabalho), ' ', '3304557', ' ', (RIGHT(exercicio_empenho,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nr_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_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` +), + frequencia_rj_v1 AS ( + SELECT id_empenho_bd, COUNT(id_empenho_bd) AS frequencia_id + FROM liquidado_municipio_rj_v1 + GROUP BY 1 + ORDER BY 2 DESC +), + liquidacao_municipio_rj_v1 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_municipio_rj_v1 l + LEFT JOIN frequencia_rj_v1 f ON l.id_empenho_bd = f.id_empenho_bd +), + liquidado_municipio_rj_v2 AS ( + SELECT + (SAFE_CAST(Exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (Data)) AS INT64)) AS mes, + SAFE_CAST (Data AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (UG AS STRING) AS orgao, + SAFE_CAST (UO AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (EmpenhoExercicio AS STRING) AS numero_empenho, + SAFE_CAST (CONCAT(Liquidacao, ' ', LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_liquidacao_bd, + SAFE_CAST (NULL AS STRING) AS id_liquidacao, + SAFE_CAST (Liquidacao 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 (Valor AS FLOAT64),2) AS valor_inicial + FROM `basedosdados-staging.world_wb_mides_staging.raw_despesa_ato_rj_municipio` + WHERE TipoAto = 'LIQUIDACAO' + ), + anulacao_municipio_rj_v2 AS ( + SELECT + SAFE_CAST (TipoAto AS STRING) AS TipoAto, + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) 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` + WHERE TipoAto IN ('CANCELAMENTO LIQUIDACAO', 'Cancelamento de liquidação de RPN', 'CANCELAMENTO DE RPN') + GROUP BY 1,2 +), + frequencia_rj_v2 AS ( + SELECT + id_empenho_bd, COUNT (1) AS frequencia + FROM anulacao_municipio_rj_v2 + GROUP BY 1 +), + liquidacao_municipio_rj_v2 AS ( + SELECT + l.ano, + l.mes, + l.data, + l.sigla_uf, + l.id_municipio, + l.orgao, + l.id_unidade_gestora, + l.id_empenho_bd, + l.id_empenho, + l.numero_empenho, + l.id_liquidacao_bd, + l.id_liquidacao, + l.numero, + l.nome_responsavel, + l.documento_responsavel, + CASE WHEN TipoAto = 'Cancelamento de liquidação de RPN' THEN true + WHEN TipoAto = 'CANCELAMENTO DE RPN' THEN true + ELSE false + END AS indicador_restos_pagar, + ROUND (SAFE_CAST(l.valor_inicial 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(l.valor_inicial AS FLOAT64), 2) AS valor_final + FROM liquidado_municipio_rj_v2 l + LEFT JOIN anulacao_municipio_rj_v2 a ON l.id_empenho_bd = a.id_empenho_bd + LEFT JOIN frequencia_rj_v2 f ON l.id_empenho_bd = f.id_empenho_bd +), + liquidacao_rj AS ( + SELECT + (SAFE_CAST(ano AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data)) AS INT64)) AS mes, + SAFE_CAST (data AS DATE) AS data, + 'RJ' AS sigla_uf, + SAFE_CAST (id_municipio AS STRING) AS id_municipio, + SAFE_CAST (id_orgao AS STRING) AS orgao, + SAFE_CAST (unidade_administrativa AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(numero_empenho, ' ', id_orgao, ' ', id_municipio, ' ', (RIGHT(ano,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (numero_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 (valor 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 AS FLOAT64),2) AS valor_final + FROM `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_rj` + WHERE numero_empenho IS NOT NULL +), + liquidacao_df AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (emissao)) AS INT64)) AS mes, + SAFE_CAST (emissao AS DATE) AS data, + 'DF' AS sigla_uf, + '5300108' AS id_municipio, + SAFE_CAST (codigo_ug AS STRING) AS orgao, + SAFE_CAST (codigo_gestao AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(RIGHT(nota_empenho, LENGTH(nota_empenho) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nota_empenho AS STRING) AS numero_empenho, + CASE WHEN LENGTH(nota_lancamento) = 11 THEN SAFE_CAST (CONCAT(RIGHT(nota_lancamento, LENGTH(nota_lancamento) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) END AS id_liquidacao_bd, + SAFE_CAST (NULL AS STRING) AS id_liquidacao, + SAFE_CAST (nota_lancamento AS STRING) AS numero, + 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 (valor 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 AS FLOAT64),2) AS valor_final + FROM `basedosdados-staging.world_wb_mides_staging.raw_liquidacao_df` ) + SELECT * FROM liquidacao_mg UNION ALL (SELECT * FROM liquidacao_sp) +UNION ALL (SELECT * FROM liquidacao_municipio_sp) UNION ALL (SELECT * FROM liquidacao_pe) UNION ALL (SELECT * FROM liquidacao_pr) UNION ALL (SELECT * FROM liquidacao_rs) UNION ALL (SELECT * FROM liquidacao_pb) UNION ALL (SELECT * FROM liquidacao_ce) -) +UNION ALL (SELECT * FROM liquidacao_municipio_rj_v1) +UNION ALL (SELECT * FROM liquidacao_municipio_rj_v2) +UNION ALL (SELECT * FROM liquidacao_rj) +UNION ALL (SELECT * FROM liquidacao_df) +) \ No newline at end of file diff --git a/models/world_wb_mides/world_wb_mides__pagamento.sql b/models/world_wb_mides/world_wb_mides__pagamento.sql index a071d7a7..59408e52 100644 --- a/models/world_wb_mides/world_wb_mides__pagamento.sql +++ b/models/world_wb_mides/world_wb_mides__pagamento.sql @@ -619,15 +619,249 @@ pago_pb AS ( LEFT JOIN dummies_sp d ON d.id_empenho_bd=p.id_empenho_bd LEFT JOIN frequencia_pg_sp f ON f.id_pagamento_bd=p.id_pagamento_bd GROUP BY 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 +), + pagamento_municipio_sp AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data_empenho)) AS INT64)) AS mes, + SAFE_CAST (data_empenho AS DATE) AS data, + 'SP' AS sigla_uf, + '3550308' AS id_municipio, + SAFE_CAST (codigo_orgao AS STRING) AS orgao, + SAFE_CAST (codigo_unidade AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(codigo_orgao), ' ', TRIM(codigo_unidade), ' ', '3550308', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (id_empenho AS STRING) AS id_empenho, + SAFE_CAST (nr_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 (razao_social 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 (codigo_fonte_recurso AS STRING) AS fonte, + ROUND(SAFE_CAST (pago 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 (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` +), + pago_municipio_rj_v1 AS ( + SELECT + SAFE_CAST(exercicio_empenho AS INT64) AS ano, + SAFE_CAST(NULL AS INT64) AS mes, + SAFE_CAST (NULL AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (orgao_programa_trabalho AS STRING) AS orgao, + SAFE_CAST (unidade_programa_trabalho AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(nr_empenho, ' ', TRIM(orgao_programa_trabalho), ' ', TRIM(unidade_programa_trabalho), ' ', '3304557', ' ', (RIGHT(exercicio_empenho,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nr_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 (favorecido AS STRING) AS nome_credor, + SAFE_CAST (codigo_favorecido AS STRING) AS documento_credor, + SAFE_CAST (NULL AS BOOL) AS indicador_restos_pagar, + SAFE_CAST (fonte_recursos 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_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` +), + frequencia_rj_v1 AS ( + SELECT id_empenho_bd, COUNT(id_empenho_bd) AS frequencia_id + FROM pago_municipio_rj_v1 + GROUP BY 1 + ORDER BY 2 DESC +), + pagamento_municipio_rj_v1 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_municipio_rj_v1 p + LEFT JOIN frequencia_rj_v1 f ON p.id_empenho_bd = f.id_empenho_bd +), + pago_municipio_rj_v2 AS ( + SELECT + (SAFE_CAST(Exercicio AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (Data)) AS INT64)) AS mes, + SAFE_CAST (Data AS DATE) AS data, + 'RJ' AS sigla_uf, + '3304557' AS id_municipio, + SAFE_CAST (UG AS STRING) AS orgao, + SAFE_CAST (UO AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (EmpenhoExercicio AS STRING) AS numero_empenho, + SAFE_CAST (CONCAT(Liquidacao, ' ', LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_liquidacao_bd, + SAFE_CAST (NULL AS STRING) AS id_liquidacao, + SAFE_CAST (Liquidacao AS STRING) AS numero_liquidacao, + SAFE_CAST (CONCAT(Pagamento, ' ', LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) AS id_pagamento_bd, + SAFE_CAST (NULL AS STRING) AS id_pagamento, + SAFE_CAST (Pagamento AS STRING) AS numero, + SAFE_CAST (NomeCredor AS STRING) AS nome_credor, + SAFE_CAST (Credor AS STRING) AS documento_credor, + SAFE_CAST (NULL AS BOOL) AS indicador_restos_pagar, + 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` + WHERE TipoAto = 'PAGAMENTO' + ), + anulacao_municipio_rj_v2 AS ( + SELECT + SAFE_CAST (TipoAto AS STRING) AS TipoAto, + SAFE_CAST (CONCAT(LEFT(EmpenhoExercicio, LENGTH(EmpenhoExercicio) - 5), ' ', TRIM(UO), ' ', TRIM(UG), ' ', '3304557', ' ', (RIGHT(Exercicio,2))) AS STRING) 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` + WHERE TipoAto IN ('CANCEL.PAGAMENTO RET.DOTAÇÃO', 'CANCEL.PAGAMENTO RET.EMPENHO', 'CANCELAMENTO DE PAGAMENTO DE RPP', 'CANCELAMENTO DE PAGAMENTO DE RPN', 'Cancelamento de RPP') + GROUP BY 1,2 +), + frequencia_rj_v2 AS ( + SELECT + id_empenho_bd, COUNT (1) AS frequencia + FROM anulacao_municipio_rj_v2 + GROUP BY 1 +), + pagamento_municipio_rj_v2 AS ( + SELECT + p.ano, + p.mes, + p.data, + p.sigla_uf, + p.id_municipio, + p.orgao, + p.id_unidade_gestora, + p.id_empenho_bd, + p.id_empenho, + p.numero_empenho, + p.id_liquidacao_bd, + p.id_liquidacao, + p.numero_liquidacao, + p.id_empenho_bd, + p.id_empenho, + p.numero, + p.nome_credor, + p.documento_credor, + CASE WHEN TipoAto = 'CANCELAMENTO DE PAGAMENTO DE RPP' THEN true + WHEN TipoAto = 'CANCELAMENTO DE PAGAMENTO DE RPN' THEN true + WHEN TipoAto = 'Cancelamento de RPP' THEN true + ELSE false + END AS indicador_restos_pagar, + p.fonte, + ROUND (SAFE_CAST(p.valor_inicial 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(p.valor_inicial AS FLOAT64), 2) AS valor_final, + ROUND (SAFE_CAST(p.valor_inicial AS FLOAT64), 2) AS valor_liquido_recebido + FROM pago_municipio_rj_v2 p + LEFT JOIN anulacao_municipio_rj_v2 a ON p.id_empenho_bd = a.id_empenho_bd + LEFT JOIN frequencia_rj_v2 f ON p.id_empenho_bd = f.id_empenho_bd +), + pagamento_rj AS ( + SELECT + (SAFE_CAST(ano AS INT64)) AS ano, + (SAFE_CAST(EXTRACT(MONTH FROM DATE (data)) AS INT64)) AS mes, + SAFE_CAST (data AS DATE) AS data, + 'RJ' AS sigla_uf, + SAFE_CAST (id_municipio AS STRING) AS id_municipio, + SAFE_CAST (id_orgao AS STRING) AS orgao, + SAFE_CAST (unidade_administrativa AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(numero_empenho, ' ', id_orgao, ' ', id_municipio, ' ', (RIGHT(ano,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (numero_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 (credor AS STRING) AS nome_credor, + SAFE_CAST (NULL AS STRING) AS documento_credor, + SAFE_CAST (NULL AS BOOL) AS indicador_restos_pagar, + SAFE_CAST (fonte AS STRING) AS fonte, + ROUND(SAFE_CAST (valor 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 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` + WHERE numero_empenho IS NOT NULL +), + pagamento_df AS ( + SELECT + (SAFE_CAST(exercicio AS INT64)) AS ano, + SAFE_CAST(SUBSTRING(emissao,-7,2) AS INT64) AS mes, + SAFE_CAST (CONCAT(SUBSTRING(emissao,-4),'-',SUBSTRING(emissao,-7,2),'-',SUBSTRING(emissao,1,2)) AS DATE) AS data, + 'DF' AS sigla_uf, + '5300108' AS id_municipio, + SAFE_CAST (codigo_ug AS STRING) AS orgao, + SAFE_CAST (codigo_gestao AS STRING) AS id_unidade_gestora, + SAFE_CAST (CONCAT(RIGHT(nota_empenho, LENGTH(nota_empenho) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) AS id_empenho_bd, + SAFE_CAST (NULL AS STRING) AS id_empenho, + SAFE_CAST (nota_empenho AS STRING) AS numero_empenho, + CASE WHEN LENGTH(nota_lancamento) = 11 THEN SAFE_CAST (CONCAT(RIGHT(nota_lancamento, LENGTH(nota_lancamento) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) END AS id_liquidacao_bd, + SAFE_CAST (NULL AS STRING) AS id_liquidacao, + SAFE_CAST (nota_lancamento AS STRING) AS numero_liquidacao, + CASE WHEN LENGTH(numero_ordem_bancaria) = 11 THEN SAFE_CAST (CONCAT(RIGHT(numero_ordem_bancaria, LENGTH(numero_ordem_bancaria) - 6), ' ', codigo_ug, ' ', codigo_gestao, ' ', '5300108', ' ', (RIGHT(exercicio,2))) AS STRING) END AS id_pagamento_bd, + SAFE_CAST (NULL AS STRING) AS id_pagamento, + SAFE_CAST (numero_ordem_bancaria AS STRING) AS numero, + SAFE_CAST (credor AS STRING) AS nome_credor, + SAFE_CAST (cnpj_cpf_credor AS STRING) AS documento_credor, + CASE WHEN ano_ordem_bancaria != ano_nota_empenho THEN true ELSE false END AS indicador_restos_pagar, + SAFE_CAST (NULL AS STRING) AS fonte, + ROUND(SAFE_CAST (REPLACE(valor_final_x, ',', '.') AS FLOAT64),2) AS valor_inicial, + ROUND(SAFE_CAST (REPLACE(valor_cancelado, ',', '.') AS FLOAT64),2) AS valor_anulacao, + ROUND(SAFE_CAST (0 AS FLOAT64),2) AS valor_ajuste, + ROUND(SAFE_CAST (REPLACE(valor_final_x, ',', '.') AS FLOAT64) - SAFE_CAST (REPLACE(valor_cancelado, ',', '.') AS FLOAT64),2) AS valor_final, + ROUND(SAFE_CAST (REPLACE(valor_final_x, ',', '.') AS FLOAT64) - SAFE_CAST (REPLACE(valor_cancelado, ',', '.') AS FLOAT64),2) AS valor_liquido_recebido, + FROM `basedosdados-staging.world_wb_mides_staging.raw_pagamento_df` ) SELECT * FROM pagamento_mg UNION ALL (SELECT * FROM pagamento_sp) +UNION ALL (SELECT * FROM pagamento_municipio_sp) UNION ALL (SELECT * FROM pagamento_pe) UNION ALL (SELECT * FROM pagamento_pr) UNION ALL (SELECT * FROM pagamento_rs) UNION ALL (SELECT * FROM pagamento_pb) UNION ALL (SELECT * FROM pagamento_ce) -) +UNION ALL (SELECT * FROM pagamento_municipio_rj_v1) +UNION ALL (SELECT * FROM pagamento_municipio_rj_v2) +UNION ALL (SELECT * FROM pagamento_rj) +UNION ALL (SELECT * FROM pagamento_df) +) \ No newline at end of file