creating 12 month rolling total
Hello,
I'm trying to create a report where there's a field that shows totals for the previous 12 months.
This is my select statement:
select YEAR(CLASSICMODELS.ORDERS.ORDERDATE) AS "YEAR", MONTH(CLASSICMODELS.ORDERS.ORDERDATE) "MONTH"
, sum(CLASSICMODELS.ORDERDETAILS.QUANTITYORDERED*CLASSICMODELS.ORDERDETAILS.PRICEEACH) as Sales
from CLASSICMODELS.ORDERS,CLASSICMODELS.ORDERDETAILS
where CLASSICMODELS.ORDERS.ORDERNUMBER = CLASSICMODELS.ORDERDETAILS.ORDERNUMBER
group by YEAR(CLASSICMODELS.ORDERS.ORDERDATE), MONTH(CLASSICMODELS.ORDERS.ORDERDATE)
I also included a computed column to give me the running count, and an aggregation element for the running sum, which give me the following results:
YEAR | MONTH | SALES |RUNNING_COUNT | RUNNING_SUM
2003 | 1 | $116,692.77 | 1 | $116,692.77
2003 | 2 | $128,403.64 | 2 | $245,096.41
2003 | 3 | $160,517.14 | 3 | $405,613.55
2003 | 4 | $185,848.59 | 4 | $591,462.14
2003 | 5 | $179,435.55 | 5 | $770,897.69
2003 | 6 | $150,470.77 | 6 | $921,368.46
2003 | 7 | $201,940.36 | 7 | $1,123,308.82
2003 | 8 | $178,257.11 | 8 | $1,301,565.93
2003 | 9 | $236,697.85 | 9 | $1,538,263.78
2003 | 10 | $514,336.21 | 10 | $2,052,599.99
2003 | 11 | $988,025.15 | 11 | $3,040,625.14
2003 | 12 | $276,723.25 | 12 | $3,317,348.39
2004 | 1 | $292,385.21 | 13 | $3,493,040.83
2004 | 2 | $289,502.84 | 14 | $3,899,236.44
2004 | 3 | $217,691.26 | 15 | $4,116,927.70
What I'm actually looking for, though, is for the running_sum= sum(sales) where the running count is between the (current record - 12) and the current record. The output I'm looking for is below:
YEAR | MONTH | SALES |RUNNING_COUNT | RUNNING_SUM
2003 | 1 | $116,692.77 | 1 | $116,692.77
2003 | 2 | $128,403.64 | 2 | $245,096.41
2003 | 3 | $160,517.14 | 3 | $405,613.55
2003 | 4 | $185,848.59 | 4 | $591,462.14
2003 | 5 | $179,435.55 | 5 | $770,897.69
2003 | 6 | $150,470.77 | 6 | $921,368.46
2003 | 7 | $201,940.36 | 7 | $1,123,308.82
2003 | 8 | $178,257.11 | 8 | $1,301,565.93
2003 | 9 | $236,697.85 | 9 | $1,538,263.78
2003 | 10 | $514,336.21 | 10 | $2,052,599.99
2003 | 11 | $988,025.15 | 11 | $3,040,625.14
2003 | 12 | $276,723.25 | 12 | $3,317,348.39
2004 | 1 | $292,385.21 | 13 | $3,493,040.83
2004 | 2 | $289,502.84 | 14 | $3,654,140.03
2004 | 3 | $217,691.26 | 15 | $3,711,314.15
Any help on how I can build this is appreciated.
Thanks!