Douglas,
Yo tengo un report igual al que necesitas y lo hice de esta manera, el primer query que te envie cree una vista con ese query llamada comprobante,
luego hice este SP.
USE [SBO]
GO
/****** Object: StoredProcedure [dbo].[SBO_SP_LIBRO]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SBO_SP_LIBRO]
(@Cuenta_Desde nvarchar(210), @cuenta_hasta nvarchar(120),
@fecha_desde date, @fecha_hasta date ) AS
declare @fecha_ini_ejercicio nvarchar(20)
declare @Fecha_saldo_ini date
declare @fechaini nvarchar(20)
set @fecha_ini_ejercicio=YEAR(@fecha_hasta)
set @Fecha_saldo_ini = DATEADD(DAY,-1,@fecha_desde )
set @fechaini='01'+'-'+'01'+'-'+@fecha_ini_ejercicio
if exists (select * from tempdb..sysobjects where name= '##TGASTOS')
begin
DROP TABLE ##TGASTOS
end
IF (SELECT TOP 1 FormatCode FROM Comprobantes where TransType = -2 and Year(RefDate) = Year(@fecha_desde)) IS NULL
BEGIN
SELECT * INTO ##TGASTOS FROM (Select OACT.FormatCode as Cuenta, OACT.AcctName As Nombre, 00 as Comprob,@Fecha_saldo_ini as taxdate,
isnull(Comprobantes.LineMemo,'Saldo Anterior') as Glosa, isnull(Comprobantes.Refdate,@Fecha_saldo_ini) as Fecha,
sum(isnull((round(comprobantes.debit,0)),0)) as Débito, sum(isnull((round(comprobantes.credit,0)),0)) as Crédito, Max(comprobantes.TransType) as TransType,'' as Proyecto
from
(select comprobantes.FOrmatCode,comprobantes.AcctName, 00 as Comprob,@Fecha_saldo_ini as taxdate, 'Saldo Anterior' as LineMemo, @Fecha_saldo_ini as refdate,
sum(round(comprobantes.debit,0)) as Debit, sum(round(comprobantes.credit,0)) as Credit, Max(comprobantes.TransType) as Transtype,'' as Proyecto
from Comprobantes inner join OJDT on comprobantes.transid=ojdt.number where comprobantes.refdate>=@fechaini and comprobantes.Refdate < @fecha_desde and
comprobantes.formatcode is not null group by comprobantes.FOrmatCode,comprobantes.AcctName) as Comprobantes right join OACT on
Comprobantes.FOrmatcode = OACT.formatcode where OACT.Postable = 'Y' AND OACT.formatcode is not null
AND OACT.formatcode BETWEEN @Cuenta_Desde and @cuenta_hasta and oact.FormatCode!='610141416'group by
OACT.Formatcode,OACT.AcctName,Comprobantes.Refdate,Comprobantes.LineMemo
UNION ALL Select comprobantes.FormatCode,comprobantes.AcctName,OJDT.ref2,OJDT.taxdate,
case when comprobantes.transtype='19' then isnull((select cardname from ORPC where docnum=OJDT.BaseRef),COMPROBANTES.LINEMEMO) else isnull((select cardname from OPCH where docnum=OJDT.baseref),COMPROBANTES.LINEMEMO) end as linememo,
comprobantes.refdate, (round(comprobantes.debit,0)) as debit, (round(comprobantes.credit,0)) as credit, comprobantes.TransType ,comprobantes.Project as Proyecto
From Comprobantes inner join OJDT on comprobantes.transid=ojdt.number
where ((comprobantes.refdate > @Fecha_desde and comprobantes.refdate < @fecha_hasta ) OR (comprobantes.refdate = @Fecha_desde and comprobantes.TransType <> -2 ) OR ( comprobantes.refdate = @fecha_hasta and comprobantes.TransType <> -3))
and ( formatcode BETWEEN @Cuenta_desde and @cuenta_hasta) and FormatCode!='610141416') as a order by 1,5,3
END ELSE
SELECT * INTO ##GASTOS FROM (Select OACT.FormatCode as Cuenta, OACT.AcctName As Nombre, 00 as Comprob, @Fecha_saldo_ini as taxdate,
isnull(Comprobantes.LineMemo,'Saldo Anterior') as Glosa, isnull(Comprobantes.Refdate,@Fecha_saldo_ini) as Fecha,
sum(isnull((round(debit,0)),0)) as Débito, sum(isnull((round(credit,0)),0)) as Crédito, isnull(Max(TransType),0) as TransType ,'' as Proyecto
from
(select comprobantes.FOrmatCode,comprobantes.AcctName,00 as Comprob,@Fecha_saldo_ini as Fechadoc,'Saldo Anterior' as LineMemo,@Fecha_saldo_ini as refdate,
sum(round(ISNULL(comprobantes.debit,0),0)) as Debit, sum(round(ISNULL(comprobantes.credit,0),0)) as Credit, isnull(Max(comprobantes.TransType),0) as Transtype ,'' as Proyecto
from Comprobantes inner join OJDT on comprobantes.transid=ojdt.number where
((comprobantes.Refdate >= @fecha_ini_ejercicio and comprobantes.refdate < @Fecha_desde and comprobantes.Transtype <> -2 AND comprobantes.formatcode is not null ) OR
(comprobantes.Refdate = @fecha_ini_ejercicio and comprobantes.Transtype = -2) and comprobantes.formatcode is not null)
group by comprobantes.FOrmatCode,comprobantes.AcctName) as Comprobantes right join OACT on
Comprobantes.FOrmatcode = OACT.formatcode where OACT.Postable = 'Y' AND OACT.formatcode is not null
AND OACT.formatcode BETWEEN @Cuenta_Desde and @cuenta_hasta and oact.FormatCode!='610141416' group by
OACT.Formatcode, OACT.AcctName, Comprobantes.Refdate, Comprobantes.LineMemo
UNION ALL
Select comprobantes.FormatCode, comprobantes.AcctName,OJDT.refdate,OJDT.taxdate, comprobantes.LineMemo, comprobantes.refdate, (round( comprobantes.debit,0)) AS debit, (round( comprobantes.credit,0)) AS credit, comprobantes.TransType ,comprobantes.Project as Proyecto
From Comprobantes inner join OJDT on comprobantes.transid=ojdt.number
where (( comprobantes.refdate > @Fecha_desde and comprobantes.refdate < @fecha_hasta ) OR ( comprobantes.refdate = @Fecha_desde and comprobantes.TransType <> -2 )
OR ( comprobantes.refdate = @fecha_hasta and comprobantes.TransType <> -3))
and ( formatcode BETWEEN @Cuenta_desde and @cuenta_hasta) and FormatCode!='610141416') as a order by 1,5,3
SELECT * FROM ##TGASTOS
Luego en crystal tienes es que hacer un grupo por cuenta y sumatorias por grupo para que te de el saldo acumulado por cuenta.
Saludos,
Wuilmer Venegas