Cumulative Sum Query

V

Vantastic

Hi
I need to run a cumulative total in a query based on expenditure per month
so that I can plot two lines on a chart, monthly expenditure and cumulative
expenditure.

Columns would be Month, Value, and then cumulative value... i have had no
success writing a query to do this, yet its a simple function to perform in
excel :)

Appreciate any input.

TIA,
 
R

Roger Carlson

D

Daryl S

Vantastic -

I will assume you have a table with expenditures in it by date. If they are
already summed by month it would be easier. The trick to doing this with one
query is to add a table with the YearMonths in them. For this code, my
YearMonths table contains a single field called YearMonth, which is a text
value and the primary key. The values look like "2009_01","2009_02", etc.

I assume a table called Expenditures has the fields [ExpenditureDate] and
[Expenditure] in them. This query will show both the monthly expenditures
and the cumulative expenditures, starting the accumulation over each year.


SELECT YearMonths.YearMonth, Sum(IIf(Year([ExpenditureDate]) & "_" &
Format(Month([ExpenditureDate]),"00")=[YearMonth] And
Year([ExpenditureDate])=Val(Left([YearMonth],4)),[Expenditure],0)) AS [Month
Expenditure], Sum(IIf(Year([ExpenditureDate]) & "_" &
Format(Month([ExpenditureDate]),"00")<=[YearMonth] And
Year([ExpenditureDate])=Val(Left([YearMonth],4)),[Expenditure],0)) AS
[Cumulative Expenditure]
FROM Expenditures, YearMonths
GROUP BY YearMonths.YearMonth
ORDER BY YearMonths.YearMonth;

The trick is to sum all expenditures by month when the year/month matches
the YearMonth value in the new table to get the monthly expenditures, and to
sum all expenditures for any YearMonths less than or equal to the current
YearMonth, but only for the current year. For this to work, the YearMonth
format chosen for the new table must sort properly by year and then month.

Hope this helps.
 
V

Vantastic

Thanks Daryl, with a little modification that worked well. Much appreciated.

Daryl S said:
Vantastic -

I will assume you have a table with expenditures in it by date. If they are
already summed by month it would be easier. The trick to doing this with one
query is to add a table with the YearMonths in them. For this code, my
YearMonths table contains a single field called YearMonth, which is a text
value and the primary key. The values look like "2009_01","2009_02", etc.

I assume a table called Expenditures has the fields [ExpenditureDate] and
[Expenditure] in them. This query will show both the monthly expenditures
and the cumulative expenditures, starting the accumulation over each year.


SELECT YearMonths.YearMonth, Sum(IIf(Year([ExpenditureDate]) & "_" &
Format(Month([ExpenditureDate]),"00")=[YearMonth] And
Year([ExpenditureDate])=Val(Left([YearMonth],4)),[Expenditure],0)) AS [Month
Expenditure], Sum(IIf(Year([ExpenditureDate]) & "_" &
Format(Month([ExpenditureDate]),"00")<=[YearMonth] And
Year([ExpenditureDate])=Val(Left([YearMonth],4)),[Expenditure],0)) AS
[Cumulative Expenditure]
FROM Expenditures, YearMonths
GROUP BY YearMonths.YearMonth
ORDER BY YearMonths.YearMonth;

The trick is to sum all expenditures by month when the year/month matches
the YearMonth value in the new table to get the monthly expenditures, and to
sum all expenditures for any YearMonths less than or equal to the current
YearMonth, but only for the current year. For this to work, the YearMonth
format chosen for the new table must sort properly by year and then month.

Hope this helps.

--
Daryl S


Vantastic said:
Hi
I need to run a cumulative total in a query based on expenditure per month
so that I can plot two lines on a chart, monthly expenditure and cumulative
expenditure.

Columns would be Month, Value, and then cumulative value... i have had no
success writing a query to do this, yet its a simple function to perform in
excel :)

Appreciate any input.

TIA,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top