Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cumulative Data query in Access 1

Status
Not open for further replies.

cogivina

Programmer
Jun 3, 2003
36
US
Hi,
I have this problem and I don't know how to write a query in MS access. I'd like to create the cumulative sales for every following month based on the table below:

Year Mos Sales
2000 1 55
2000 3 78
2000 6 62
2000 10 45
2001 2 36
2001 12 59

----------------------
mos1 = 55
mos2 = sale(mos1) + sale(mos2) = 55+0 = 55
mos3 = mos2 + sale(mos3) = 55+0+78 = 133, ....

The cumulative sale table:

Year Mos Sales
2000 1 55
2000 2 55
2000 3 133
2000 4 133
2000 5 133
2000 6 195
2000 7 195
2000 8 195
2000 9 195
2000 10 257
2000 11 257
2000 12 257
2001 1 0
2001 2 36
2001 3 36
.............
2001 12 95

Thanks in advance.

 
Hi, I have what I think is a solution.
Essentially I created a query for the current months figures, a query for the previous months figures, then linked and totaled.
It looks a little complicated, but does the trick if I understood the requirements okay.

If not.... ho hum !

Now to figure out if i cannt attach the file!
 
Okay, the upload flopped. so here are is the sql statements for the queries.

Query1

SELECT ([Year]-2000)*12+[Mos] AS YrMos, ([Year]-2000)*12+[Mos]-1 AS YrMos2, tblMOS.Year, tblMOS.Mos, tblMOS.Sales AS SalesCurr
FROM tblMOS;

Query2

SELECT ([Year]-2000)*12+[Mos] AS YrMos3, IIf([Sales]=0,0,[Sales]) AS SalesPrevious
FROM tblMOS;

Query3

SELECT Query1.YrMos, Query1.YrMos2, Query2.YrMos3, Query1.Year, Query1.Mos, Query1.SalesCurr, Query2.SalesPrevious, IIf([SalesPrevious]<>0,[SalesPrevious],0) AS SalesPrevious2, [SalesCurr]+[SalesPrevious2] AS CumulativeSales
FROM Query1 LEFT JOIN Query2 ON Query1.YrMos2 = Query2.YrMos3;


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top