???????????????????????????
quizas esto te sirva
Código SQL:
Ver originalSELECT Fecha,NumCta,Nombre,SegNeg,Cargo,Abono, Saldo_Inicial
FROM
(
SELECT NumCta, Saldo_Inicial FROM(
SELECT Fecha,NumCta,Nombre,SegNeg,Cargo,Abono, cargo-abono AS Saldo_Inicial FROM(
SELECT Fecha,NumCta,Nombre,SegNeg, SUM(ISNULL (Cargo,0)) AS cargo, SUM(ISNULL (Abono,0)) AS Abono FROM
(
SELECT M.Fecha,C.Codigo AS NumCta,C.Nombre AS Nombre,S.Codigo AS SegNeg, SUM(CASE WHEN TipoMovto = 0 THEN Importe END) AS Cargo, SUM (CASE WHEN TipoMovto = 1 THEN Importe END) AS Abono
FROM SegmentosNegocio S
INNER JOIN MovimientosPoliza M ON M.IdSegNeg = S.Id JOIN Cuentas C ON C.Id = M.IdCuenta
WHERE Fecha BETWEEN '20120910' AND '20120930'
GROUP BY Fecha,C.Codigo,C.Nombre,S.Codigo
) AS MovimientosPoliza
GROUP BY Fecha,NumCta,Nombre,SegNeg
) AS MovimientosPoliza
) t1
LEFT JOIN(
SELECT Fecha,NumCta,Nombre,SegNeg,Cargo,AbonoFROM(
SELECT Fecha,NumCta,Nombre,SegNeg, SUM(ISNULL (Cargo,0)) AS cargo, SUM(ISNULL (Abono,0)) AS Abono FROM
(
SELECT M.Fecha,C.Codigo AS NumCta,C.Nombre AS Nombre,S.Codigo AS SegNeg, SUM(CASE WHEN TipoMovto = 0 THEN Importe END) AS Cargo, SUM (CASE WHEN TipoMovto = 1 THEN Importe END) AS Abono
FROM SegmentosNegocio S
INNER JOIN MovimientosPoliza M ON M.IdSegNeg = S.Id JOIN Cuentas C ON C.Id = M.IdCuenta
WHERE Fecha BETWEEN '20121010' AND '20121030'
GROUP BY Fecha,C.Codigo,C.Nombre,S.Codigo
) AS MovimientosPoliza
GROUP BY Fecha,NumCta,Nombre,SegNeg
) AS MovimientosPoliza) AS t2 ON (t1.NumCta=t2.NumCta)
) AS completo
saludos!