473,328 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,328 software developers and data experts.

Cumulative (running totals)

Hi There,

I have a query witch gives me the following result:

Period NumberOfItems
1 13
2 2
3 1
4 1
5 1
6 0
7 1
8 2
9 1
10 6
11 0
12 2
13 0

But now i want the number of items to be cumulative aswell so like this:

Period NumberOfItems Cumulative
1 13 13
2 2 15
3 1 16
4 1 17
5 1 18
6 0 18
7 1 19
8 2 21
9 1 22
10 6 28
11 0 28
12 2 30
13 0 30

How can i do this?

Thnx in advance,
Victor
Nov 13 '05 #1
1 2452
Hi Victor,

For a cumulative column you could use a sub query.

For a sub query the SQL would be something like:-

SELECT Period, NumberOfItems,
(SELECT Sum(NumberOfItems) FROM MyTable T2
WHERE T2.Period <= T1.Period) As Cumulative
FROM MyTable T1

T1 and T2 are aliases for the table you are using.

It will only work where you have a column with unique entries such as your
'Period' Column.
If the original table has years as well as periods you will need to add a
WHERE clause to the main query and extend the WHERE clause on the sub query.
e.g.

SELECT Period, NumberOfItems,
(SELECT Sum(NumberOfItems) FROM MyTable T2
WHERE T2.Period <= T1.Period AND T2.Year=2004) As Cumulative
FROM MyTable T1
WHERE T1.Year=2004

Cheers,
Peter
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Larkin | last post by:
I am pulling my hair out trying to solve this one (presumably because I am not particularly trained or skilled at access!) I have a query which summarises the variances that each of my sales...
4
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
6
by: KashMarsh | last post by:
Trying to show running totals on a report, except it needs to show one total amount and values being subtracted from it. For example, the report shows a Total Inventory amount (TotInvAmt). And...
3
by: papaja | last post by:
Hello, My table has 2 columns with some numbers: col1 col2 --------------- 5 0 0 5 0 10 2 0 8 0
0
by: James Hallam | last post by:
I have searched through the news groups and found many threads close to what I want but cannot get any of them to work... I have a table with expenses and invoices, what I want is a bar chart...
9
by: MikeSA | last post by:
Hi I a trying to create a chart that reflects monthly cumulative totals from a query. The query fields show sales opportunities forecasted invoice date (OppForInvDate), Opportunity Description and...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
1
by: medphillips | last post by:
I am new to SQL so forgive me if this is an SQL 101 question. I have 2 tables relating to vehicle use by employees. I am creating a report to give me totals on number of vehicles, total stops,...
1
by: cbellew | last post by:
Hi guys, i'm looking to create a report with a table showing totals (running and cumulative) of education sessions attend by the staff at a hospital. I'm trying to get the table to show something...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.