Actually found a way to do it in a query that's pretty painless my table name is TBLTEST
ID |
MainType |
SubType |
MonthEnd |
MonthValue |
1 |
A |
A1 |
1/31/2015 |
1 |
2 |
A |
A2 |
2/28/2015 |
1 |
3 |
A |
A3 |
3/31/2015 |
1 |
4 |
B |
B1 |
11/30/2014 |
3 |
5 |
B |
B1 |
12/31/2014 |
2 |
6 |
B |
B1 |
1/31/2015 |
1 |
7 |
B |
B1 |
2/28/2015 |
1 |
8 |
B |
B1 |
3/31/2015 |
1 |
9 |
B |
B1 |
3/30/2015 |
1 |
10 |
B |
B1 |
5/31/2015 |
1 |
11 |
B |
B1 |
6/30/2015 |
1 |
12 |
B |
B1 |
7/31/2015 |
1 |
13 |
B |
B1 |
8/31/2015 |
1 |
14 |
B |
B1 |
9/30/2015 |
1 |
15 |
B |
B1 |
10/31/2015 |
1 |
16 |
B |
B1 |
11/30/2015 |
1 |
17 |
B |
B1 |
12/31/2015 |
1 |
18 |
B |
B1 |
1/31/2016 |
1 |
This is my table, note the field names are different than yours to prevent using reserved words
Create this query:
Code:
SELECT tblTest.ID, tblTest.MainType, tblTest.SubType, tblTest.MonthEnd, tblTest.MonthValue, DateAdd("yyyy",-1,[monthend])+1 AS YearStartFROM tblTest;
Name this query QrySub
Create this query:
Code:
SELECT qrySub.MainType, qrySub.SubType, DatePart("yyyy",[qrysub]![monthend]) AS YearSort, DatePart("m",[qrysub]![monthend]) AS MonthSort, MonthName(DatePart("m",[qrysub]![monthend])) AS MonthDesc, qrySub.MonthEnd, qrySub.MonthValue, Sum(IIf([tblTest]![monthend] Between [qrysub]![yearstart] And [qrysub]![monthend],[tbltest]![monthvalue],0)) AS Rolling12Sum, Sum(IIf([tblTest]![monthend] Between [qrysub]![yearstart] And [qrysub]![monthend],1,0)) AS Rolling12Count
FROM qrySub LEFT JOIN tblTest ON (qrySub.SubType = tblTest.SubType) AND (qrySub.MainType = tblTest.MainType)
GROUP BY qrySub.MainType, qrySub.SubType, DatePart("yyyy",[qrysub]![monthend]), DatePart("m",[qrysub]![monthend]), MonthName(DatePart("m",[qrysub]![monthend])), qrySub.MonthEnd, qrySub.MonthValue;
It has all the numbers you want except the average but that's just division by two of the fields when you go to produce a report.