Ver Mensaje Individual
  #2 (permalink)  
Antiguo 05/08/2013, 13:43
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 3 meses
Puntos: 774
Respuesta: Problemas con un Extracto: DEBE, HABER y SALDO

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. accdate datetime,
  4. DebitCredit VARCHAR(20),
  5. Account INT,
  6. Amount INT
  7. )
  8.  
  9. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110101','D',11200,1500)
  10. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110101','C',11200,60)
  11. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','D',11200,400)
  12. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,100)
  13. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,300)
  14. INSERT INTO #temp (AccDate,DebitCredit,Account,Amount) VALUES ('20110102','C',11200,250)
  15.  
  16.  
  17.  
  18. SELECT t1.accdate,t1.account,t1.DebitCredit,t1.amount,SUM(t2.amount) suma
  19. FROM(
  20. SELECT ROW_NUMBER() OVER(partition BY account ORDER BY account) AS rn, * FROM #temp
  21. ) AS t1 INNER JOIN
  22. (
  23. SELECT ROW_NUMBER() OVER(partition BY account ORDER BY account) AS rn, * FROM #temp
  24. ) AS t2 ON (t1.rn>=t2.rn)
  25. GROUP BY t1.accdate,t1.Account,t1.rn,t1.Amount,t1.DebitCredit

Prueba con esto, encontre la idea en este foro:

http://stackoverflow.com/questions/2...cumulative-sum

saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me