Fórum Saldo de debito e credito na mesma tabela #620800

05/12/2023

0

Preciso listar o total de debito, total de credito e saldo agrupados por conta e mês.

Porém não estou conseguindo fazer o agrupamento por mês e por conta dessas somas.

Realizei o seguinte:

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
select
    YEAR (DATA) AS ANO,
    FORMAT( data, 'dd/MM/yyyy', 'en-US' ) AS MÊS,
    'COD UNIDADE'=1, 'COD CENTRO DE CUSTO'=1,CONTA, total_debito 'TOTAL DEBITO', total_credito 'TOTAL CREDITO',
    abs(total_debito - total_credito) SALDO
     
from (
    select
        conta,data,
        sum(case when tipo='D' then valor else 0 end) total_debito,
        sum(case when tipo='C' then valor else 0 end) total_credito
 
    from (
            select 'D' tipo, s.CONTADEB conta, valor, DATA
            from SUBLANCA s
            where not CONTADEB is null
            union all
            select 'C' tipo, s2.CONTACRED, valor, DATA
            from SUBLANCA s2
            where not CONTACRED is null
 
    ) tab
 
where LEFT (CONTA,1) in ('3','4'AND DATA between '01/08/2023' and '30/09/2023'
   group by
        conta, DATA
         
) resultado
 
order by conta



Exemplo:

CONTA | DATA | TOTAL DEBITO | TOTAL CREDITO | SALDO

300007 12/09/23 2000 1000 1000
300007 15/09/23 5000 2000 3000
300008 15/08/23 0000 1000 -1000

Preciso agrupar essas linhas por mês, ou seja para ser exibido que no mês 09 a conta 300007 o total de debito é 7000, total de crédito é 3000 e o saldo é 4000, E para a conta 30008 no mês 08 o total de debito é 0, total de crédito é 1000, tendo como saldo -1000.

Tentei agrupar por month (data), mas não consegui. Alguma dica de como posso fazer esse agrupamento mensal ?
Mylena

Mylena

Responder

Post mais votado

05/12/2023

tente assim:
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
select
    CONTA,
    anomes,
    1 'COD UNIDADE',
    1 'COD CENTRO DE CUSTO',
    total_debito 'TOTAL DEBITO',
    total_credito 'TOTAL CREDITO',
    abs(total_debito - total_credito) SALDO -- abs() mesmo?
from (
    select
        conta,
        convert(varchar(6), data, 112) anomes,
        sum(case when tipo='D' then valor else 0 end) total_debito,
        sum(case when tipo='C' then valor else 0 end) total_credito
  
    from (
            select 'D' tipo, s.CONTADEB conta, valor, DATA
            from SUBLANCA s
            where not CONTADEB is null
            union all
            select 'C' tipo, s2.CONTACRED, valor, DATA
            from SUBLANCA s2
            where not CONTACRED is null
  
        ) tab
    where
        LEFT(CONTA,1) in ('3','4')
        AND DATA between '01/08/2023' and '30/09/2023'
   group by
        conta,
        convert(varchar(6), data, 112)
) resultado
order by
    conta,
    anomes

Emerson Nascimento

Emerson Nascimento
Responder

Gostei + 1

Mais Posts

06/12/2023

Mylena

Tentei com esse comando, mas agrupou somente com o ano e mês

Como faço para grupar separadamente (agrupar por ano e depois por mês), exemplo

ANO | MES | CONTA | TOTAL DEBITO | TOTAL CREDITO | SALDO

2023 09 3000007 2000 1000 1000
2023 08 3000007 5000 2000 3000
2022 08 3000007 1000 1000


Sobre o abs, já corrigi aqui, muito obrigada pela observação
Responder

Gostei + 0

07/12/2023

Emerson Nascimento

basta separar o ano e o mês
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
select
    CONTA,
    left(anomes,4) ANO,
    right(anomes,2) MES,
    1 'COD UNIDADE',
    1 'COD CENTRO DE CUSTO',
    total_debito 'TOTAL DEBITO',
    total_credito 'TOTAL CREDITO',
    abs(total_debito - total_credito) SALDO -- abs() mesmo?
from (
    select
        conta,
        convert(varchar(6), data, 112) anomes,
        sum(case when tipo='D' then valor else 0 end) total_debito,
        sum(case when tipo='C' then valor else 0 end) total_credito
   
    from (
            select 'D' tipo, s.CONTADEB conta, valor, DATA
            from SUBLANCA s
            where not CONTADEB is null
            union all
            select 'C' tipo, s2.CONTACRED, valor, DATA
            from SUBLANCA s2
            where not CONTACRED is null
   
        ) tab
    where
        LEFT(CONTA,1) in ('3','4')
        AND DATA between '01/08/2023' and '30/09/2023'
   group by
        conta,
        convert(varchar(6), data, 112)
) resultado
order by
    conta,
    anomes
Responder

Gostei + 1

07/12/2023

Mylena

Deu certinho, muito obrigada
Responder

Gostei + 0

09/09/2024

Mylena

Estou novamente com dúvida sobre esse comando:

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
select
    CONTA,
    left(anomes,4) ANO,
    right(anomes,2) MES,
    1 'COD UNIDADE',
    1 'COD CENTRO DE CUSTO',
    total_debito 'TOTAL DEBITO',
    total_credito 'TOTAL CREDITO',
    abs(total_debito - total_credito) SALDO -- abs() mesmo?
from (
    select
        conta,
        convert(varchar(6), data, 112) anomes,
        sum(case when tipo='D' then valor else 0 end) total_debito,
        sum(case when tipo='C' then valor else 0 end) total_credito
    
    from (
            select 'D' tipo, s.CONTADEB conta, valor, DATA
            from SUBLANCA s
            where not CONTADEB is null
            union all
            select 'C' tipo, s2.CONTACRED, valor, DATA
            from SUBLANCA s2
            where not CONTACRED is null
    
        ) tab
    where
        LEFT(CONTA,1) in ('3','4')
        AND DATA between '01/08/2023' and '30/09/2023'
   group by
        conta,
        convert(varchar(6), data, 112)
) resultado
order by
    conta,
    anomes


Tem alguns valores da coluna saída que estão negativos, como posso transformar esses números em positivos ?
Responder

Gostei + 0

09/09/2024

Arthur Heinrich

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
select
  ano, mes, conta, sum(debito) [total debito], sum(credito) [total credito], sum(debito)-sum(credito) saldoSALDO
from
  ( select
      year(data) ano,
      month(data) mes,
      coalesce( contadeb, contacred ) conta,
      case when contadeb is not null then valor else 0 end debito,
      case when contacred is not null then 0 else valor end credito
    from sublanca
    where
      conta >= '3' and
      conta < '5' and
      data between '01/08/2023' and '30/09/2023' ) tab
group by ano, mes, conta
order by ano, mes, conta
Responder

Gostei + 0

09/09/2024

Arthur Heinrich

Me equivoquei no case:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
select
  ano, mes, conta, sum(debito) [total debito], sum(credito) [total credito], sum(debito)-sum(credito) saldo
from
  ( select
      year(data) ano,
      month(data) mes,
      coalesce( contadeb, contacred ) conta,
      case when contadeb is not null then valor else 0 end debito,
      case when contacred is not null then valor else 0 end credito
    from sublanca
    where
      conta >= '3' and
      conta < '5' and
      data between '01/08/2023' and '30/09/2023' ) tab
group by ano, mes, conta
order by ano, mes, conta
Responder

Gostei + 0

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

Aceitar