Saldo de debito e credito na mesma tabela

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:

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:
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

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

07/12/2023

Emerson Nascimento

basta separar o ano e o mês
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

07/12/2023

Mylena

Deu certinho, muito obrigada
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