martes, 14 de mayo de 2013
Saldos Actuales y Anteriores de Cuentas Contables
WITH FACT_ACCT AS
( SELECT v.nat AS naturaleza,
f.account_id AS id_cuenta,
f.dateacct AS fecha_contable,
f.c_period_id,
COALESCE(SUM(F.AMTACCTDR), 0) AS DEBITO,
COALESCE(SUM(F.AMTACCTCR), 0) AS CREDITO
FROM fact_acct AS f
JOIN usr_v_vcc AS v ON f.account_id = v.c_elementvalue_id
WHERE f.ad_client_id = 1000001
AND f.ad_table_id = 224
AND f.line_id > 0
GROUP BY v.nat,
f.account_id,
f.dateacct,
f.c_period_id),
Movimientos_Periodo AS
(SELECT id_cuenta,
SUM(DEBITO) AS DEBITOS,
SUM(CREDITO) AS CREDITOS
FROM FACT_ACCT
WHERE fecha_contable BETWEEN '20121101' AND '20121130'
GROUP BY id_cuenta),
Movimientos_Saldos AS
(SELECT MP.id_cuenta AS CuentaID,
MP.DEBITOS,
MP.CREDITOS,
(CASE WHEN U.Nat = 'D' THEN
MP.DEBITOS - MP.CREDITOS
WHEN U.Nat = 'C' THEN
MP.CREDITOS - MP.DEBITOS
ELSE 0.000000000 END) AS SALDO
FROM Movimientos_Periodo AS MP
INNER JOIN usr_v_vcc AS U ON U.c_elementvalue_id=MP.id_cuenta) ,
Movimientos_Periodo_Anterior AS
(SELECT id_cuenta,
SUM(DEBITO) AS DEBITOS,
SUM(CREDITO) AS CREDITOS
FROM FACT_ACCT
WHERE fecha_contable BETWEEN '20121001' AND '20121031'
GROUP BY id_cuenta) ,
Movimientos_Saldos_Anterior AS
(SELECT MPA.id_cuenta AS CuentaID,
MPA.DEBITOS,
MPA.CREDITOS,
(CASE WHEN U.Nat = 'D' THEN
MPA.DEBITOS - MPA.CREDITOS
WHEN U.Nat = 'C' THEN
MPA.CREDITOS - MPA.DEBITOS
ELSE 0.000000000 END) AS SALDO_ANTERIOR
FROM Movimientos_Periodo_Anterior AS MPA
INNER JOIN usr_v_vcc AS U ON U.c_elementvalue_id=MPA.id_cuenta)
SELECT MS.CuentaID,
MS.DEBITOS,
MS.CREDITOS,
MS.SALDO,
MSA.SALDO_ANTERIOR
FROM Movimientos_Saldos AS MS
INNER JOIN Movimientos_Saldos_Anterior AS MSA ON MS.CuentaID=MSA.CuentaID
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario