Fórum Variável somando sozinha #618325

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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

Ler Mais...

Ganso Ltda

Ganso Ltda

Responder

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

Aceitar