sabato 25 settembre 2010

Oracle YTD Calculation using analytical functions (sum up monthly)

This is the script's table:

CREATE TABLE KRIMISIA.CB_TEST
(
  MONTH_REF NUMBER,
  MONTH_VALUE NUMBER
)

Table will look like this:


This is the query:

select
  MONTH_REF
  ,sum(MONTH_VALUE) over (order by MONTH_REF rows unbounded preceding) MONTHLY_VALUE_YTD
from
(
  select MONTH_REF, sum(MONTH_VALUE)as MONTH_VALUE
  from CB_TEST
  group by MONTH_REF
)

... and ...



Ciao,
Claudio