Variável somando sozinha

01/07/2022

0


Boa tarde, eu tenho uma consulta SQL feita no execute blocks e estou com um problema. No final do execute blocks eu preciso fazer a soma de todos os valores a receber e de todos os valores a pagar. Mas por algum motivo a variável está adicionando os valores de outra variável nela mesma
execute block
returns ("Mês" varchar (15), "Total a Receber" numeric (15,2), "Total a Pagar" numeric (15,2), "Saldo" numeric (15,2))
as
declare variable vMesAnt integer;
declare variable vMesAtu integer;
declare variable tipo varchar(15);
declare variable valor numeric(15,2);
declare variable totalCR numeric (15,2);
declare variable totalCP numeric (15,2);

begin
vMesAnt = 1;
      for
          select extract (month from cr.data_emissao), sum(cr.valor_aberto), 'CR'"TIPO"


          from contas_receber cr
          where cr.status = 'A'
          group by extract (month from cr.data_emissao)

          union all

          select extract (month from cp.data_emissao), sum(cp.valor_aberto), 'CP'"TIPO"
          from contas_pagar cp
          where cp.status = 'A'
          group by extract(month from cp.data_emissao)
          order by 1
          into vMesAtu, valor, tipo


      do
      begin
        if (vMesAnt <> vMesAtu) then
        begin
        suspend;
        vMesAnt = vMesAtu;
        "Total a Receber" = 0;
        "Total a Pagar" = 0;
        end

        if (tipo = 'CR') then
        "Total a Receber" = valor;
        else if (tipo = 'CP') then
        "Total a Pagar" = valor;

        if (vMesAtu = 1) then
        "Mês" = 'Janeiro';

        if (vMesAtu = 2) then
        "Mês" = 'Fevereiro';

        if (vMesAtu = 3) then
        "Mês" = 'Março';

        if (vMesAtu = 4) then
        "Mês" = 'Abril';

        if (vMesAtu = 5) then
        "Mês" = 'Maio';

        if (vMesAtu = 6) then
        "Mês" = 'Junho';

        if (vMesAtu = 7) then
        "Mês" = 'Julho';

        if (vMesAtu = 8) then
        "Mês" = 'Agosto';

        if (vMesAtu = 9) then
        "Mês" = 'Setembro';

        if (vMesAtu = 10) then
        "Mês" = 'Outubro';

        if (vMesAtu = 11) then
        "Mês" = 'Novembro';

        if (vMesAtu = 12) then
        "Mês" = 'Dezembro';

        "Saldo" = "Total a Receber" - "Total a Pagar";
        totalCR = coalesce(totalCR, 0) + coalesce("Total a Receber", 0);
        totalCP = coalesce(totalCP, 0) + coalesce("Total a Pagar", 0);
        end
        suspend;
        "Mês" = '-->Totais';
        "Total a Receber" = totalCR;
        "Total a Pagar" = totalCP;
        "Saldo" = "Total a Receber" - "Total a Pagar";
        suspend;
      end


Ganso Ltda

Ganso Ltda

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar