sql >> Base de Datos >  >> RDS >> Oracle

Total acumulado dinámico

ACTUALIZADO ¿Estás buscando esto?

CREATE VIEW vw_rain_stats
AS
  SELECT TRUNC(rain_date, 'MM') rain_date,
         SUM(amt) amt
    FROM rain_stats
   GROUP BY TRUNC(rain_date, 'MM')
;

Obtenga montos mensuales para el año 2012

SELECT rain_date,
       amt,
       SUM(amt) OVER (ORDER BY rain_date) running_amt
  FROM vw_rain_stats
 WHERE rain_date BETWEEN '01-JAN-12' AND '01-DEC-12';

Obtenga montos mensuales a partir de diciembre de 2011

SELECT rain_date,
       amt,
       SUM(amt) OVER (ORDER BY rain_date) running_amt
  FROM vw_rain_stats
 WHERE rain_date >= '01-DEC-11';

Salida de muestra:

|          RAIN_DATE | AMT | RUNNING_AMT |
------------------------------------------
| December, 01 2011  |  80 |          80 |
|  January, 01 2012  |  30 |         110 |
| February, 01 2012  |  70 |         180 |
|    March, 01 2012  | 110 |         290 |
| .................. | ... | ........... |

Aquí está SQLFiddle demostración