By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,460 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,460 IT Pros & Developers. It's quick & easy.

Create a Query that provides a running total but grouped?

P: 8
I need to create a query that produces running totals for every group within my table for example i wish to see: -

Group A

[running total] 1
[running total] 5
[running total] 9
[running total] 15

Group B

[running total] 1
[running total] 2
[running total] 20


I have worked out how to produce a running total but this will produce it for all records in the table and not split it (or reset it when it find a new group). Also this method works on the Unique ID which will fail due to the grouping required.
Sep 26 '06 #1
Share this Question
Share on Google+
16 Replies

Expert 100+
P: 1,418

Try to do a query that do the sum by your wanted groups and make Union query with your not aggregate query

Sep 26 '06 #2

P: 8
Thanks for that, I think I understand what you mean!!

But if I sum by group and then run a query on the results, won't that just give me the running total of the groups?

I am looking to list all records by group with all the fields in each group showing a running total. After I have achieved that I only want to see records in ech group that reach a certain value.

Thanks again
Sep 26 '06 #3

Expert 100+
P: 1,418
You mention that u've done the running total! How did you do it?

Maybe we can integrate sth with this?

Sep 26 '06 #4

P: 8
I used a sql query to create an alias and then compared ID with ID : -

SELECT tbl_Alias.groups, AS Expr1, (SELECT Sum([table3].[totals]) AS Total
FROM [table3]
WHERE ((([table3].[ID])<=[tbl_alias].[ID]));) AS [Running Total], tbl_Alias.totals
FROM table3 AS tbl_Alias, Table3
GROUP BY tbl_Alias.groups,, tbl_Alias.totals;

This wirks by creating a running total based on my unique ID field however I need to do this on the records within each group.

Any Ideas??
Sep 26 '06 #5

Expert 100+
P: 1,418
So it seems to be reachable using VB functions and a temporary table to store the results! This is the way that I see!

Do u want to proceed in this direction?
Sep 26 '06 #6

P: 8
I will be using the results as a transfer to MS Excel (the only reason I am using Access is because of the amount of rows required (over 65000 records). I will be happy to use any method avaliable.
Sep 26 '06 #7

Expert 100+
P: 1,418
So this have to be pasted into a module in Access:

Expand|Select|Wrap|Line Numbers
  1. Global last_criteria
  2. Global last_used
  4. Function Set_last(Values, criterias)
  5. last_criteria = nts(criterias)
  6. last_used = nts(Values)
  7. Set_last = last_used
  8. End Function
  10. Function show_last(Ref)
  11. 'Stop
  12. show_last = last_used
  13. End Function
  14. Function show_last_criteria(Ref)
  15. show_last_criteria = last_criteria
  16. End Function
For running sum for a Group your column should be:
Run_sum: IIF(show_last_criteria([Group_Name])=[Group_Name],Set_last(val(show_last([Group_Name]))+ [myvalues],[Group_Name]),Set_last([myvalues], [Group_Name]))

So you need to change in this expression:
[Group_Name] with the name of the field that contains your groups
[myvalues] with the Field on which you are running sum

From query choose make table query and give the name of the table in which will be stored the result!

Than choose Run and open the respective table!

Sep 26 '06 #8

P: 8
Thanks for the script, however I am getting a sub or function not defined.. with the nts highlighted??
Sep 26 '06 #9

P: 8
I have removed the nts references so now it looks like: -

Function Set_last(Values, criterias)
last_criteria = criterias
last_used = Values
Set_last = last_used
End Function

the code runs okay but my resulting table is duplicating the running sum?


id totals Run_sum groups
1 0.2 0.4 group1 (added 0.4 instead of 0.2)
2 0.2 0.8 group1 (added 0.4 instead of 0.2)
3 0.5 1.8 group1 (added 1.0 instead of 0.5)
4 0.7 3.2 group1 etc...
5 0.8 4.8 group1
6 0.2 5.2 group1
7 0.5 6.2 group1
8 0.1 0.2 group2
9 0.1 0.4 group2
10 0.1 0.6 group2
11 0.1 0.8 group2
12 0.1 1 group2
13 0.1 1.2 group2
Sep 26 '06 #10

Expert 100+
P: 1,418

This is because you use grouping, remove your grouping!

And any conditions under this fields!
Sep 26 '06 #11

P: 8

Great stuff, now I have all I need to start my Project!

Thank you very much.
Sep 27 '06 #12

Expert 100+
P: 1,418
Great man!!!

Have a nice day!

Sep 27 '06 #13

Narender Sagar
P: 189
Hi, I was trying to use this code in my query, but I'm also getting duplicate running sum. I'm not able to understand, which groupings I need to remove! Can you please help to fix this.
Jul 24 '17 #14

Narender Sagar
P: 189
Oh! I removed one calculated column from the query and result was perfect. Thanks, matter resolved.
Jul 24 '17 #15

Narender Sagar
P: 189
Dear all,
How to initialize the value in this code, because whenever I open this query, the values are getting multiplied exponentially.
Jul 25 '17 #16

P: 1
Thank you everyone for this thread, it has been hugely helpful to me!
Jan 22 '19 #17

Post your reply

Sign in to post your reply or Sign up for a free account.