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