SELECT A3.Cdgottl ACCT_Titulo, A4./*1NIIF_1*/Descripcion DescAcctTitulo,
A3.Cdgogrpo ACCT_Grupo, A5./*1NIIF_1*/Descripcion DescAcctGrupo,
A3.Cdgocnta ACCT_Cuenta, A6./*1NIIF_1*/Descripcion DescAcctCuenta,
A3.Cdgosbcnta ACCT_SubCuenta, A7./*1NIIF_1*/Descripcion DescAcctSubCuenta,
C.ACCT, A3.Descripcion DescACCT, C.SaldoAnterior, C.Debito, C.Credito, (C.SaldoAnterior + (C.Debito - C.Credito)) SaldoActual/*3,
C.Depto, COALESCE(L2.Descripcion, 'Sin Depto') DescDepto,
C.CCost, COALESCE(L1.Descripcion, 'Sin CCost') DescCCost3*//*2, C.ID_N, CU.Company2*/
FROM (
SELECT B.ACCT, /*3B.Depto, B.CCost CCost,3*//*2 B.ID_N ID_N,2*/
SUM(SaldoAnterior)SaldoAnterior, SUM(Debito)Debito, SUM(Credito)Credito
FROM
(
SELECT A.ACCT, /*3A.Depto, A.CCost CCost,3*//*2 A.ID_N ID_N,2*/
IIF(MIN(A.FechaGL) = -1, SUM(A.Debito), 0) - IIF(MIN(A.FechaGL) = -1, SUM(A.Credito), 0) SaldoAnterior,
IIF(MIN(A.FechaGL) = 1, SUM(A.Debito), 0) Debito,
IIF(MIN(A.FechaGL) = 1, SUM(A.Credito), 0) Credito
FROM
(
SELECT A.ACCT,
(COALESCE(G1.Debit,0)) Debito,
(COALESCE(G1.Credit,0)) Credito,
IIF(G1.Fecha < :FI, -1, 1) FechaGL/*3,
COALESCE(G1.Depto, 0) Depto, COALESCE(G1.CCost, 0) CCost3*//*2, G1.ID_N2*/
FROM ACCT A
LEFT JOIN GL G1 ON (G1.ACCT = A.ACCT)
--LEFT JOIN TipDoc TD1 ON (TD1.E=G1.E AND TD1.S=G1.S AND TD1.Clase=G1.Tipo)
WHERE A.ACCT BETWEEN :ACCT1 AND :ACCT2
--AND TD1.Tipo <> 'XX'
AND ( (G1.Fecha < :FI) OR (G1.Fecha BETWEEN :FI AND :FF) )
AND G1.E=:E
AND ( (G1.Destino=:D) OR (-1=:D) )
) A
GROUP BY A.ACCT, A.FechaGL /*3, A.Depto, A.CCost3*//*2, A.ID_N2*/
) B
GROUP BY B.ACCT /*3, B.Depto, B.CCost3*//*2, B.ID_N2*/
HAVING ((SUM(SaldoAnterior) <> 0) OR (SUM(Debito) <> 0) OR (SUM(Credito) <> 0))
) C
LEFT JOIN ACCT A3 ON (A3.ACCT = C.ACCT)
LEFT JOIN ACCT A4 ON (A4.ACCT = A3.Cdgottl)
LEFT JOIN ACCT A5 ON (A5.ACCT = A3.Cdgogrpo)
LEFT JOIN ACCT A6 ON (A6.ACCT = A3.Cdgocnta)
LEFT JOIN ACCT A7 ON (A7.ACCT = A3.Cdgosbcnta)
/*3LEFT JOIN Lista L1 ON (L1.Tipo='
CC' AND L1.Codigo=C.CCost)
LEFT JOIN Lista L2 ON (L2.Tipo='
DP' AND L2.Codigo=C.Depto)3*/
/*2LEFT JOIN Cust CU ON (CU.ID_N=C.ID_N)2*/