Saldo de debito e credito na mesma tabela
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:
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 ?
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
Curtidas 0
Melhor post
Emerson Nascimento
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
GOSTEI 1
Mais Respostas
Mylena
05/12/2023
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
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
GOSTEI 0
Emerson Nascimento
05/12/2023
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,
anomesGOSTEI 1
Mylena
05/12/2023
Deu certinho, muito obrigada
GOSTEI 0
Mylena
05/12/2023
Estou novamente com dúvida sobre esse comando:
Tem alguns valores da coluna saída que estão negativos, como posso transformar esses números em positivos ?
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 ?
GOSTEI 0
Arthur Heinrich
05/12/2023
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
GOSTEI 0
Arthur Heinrich
05/12/2023
Me equivoquei no case:
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
GOSTEI 0