Solved

Access- running total query

Posted on 2009-04-02
6
1,265 Views
Last Modified: 2012-05-06
I am an Access novice. I need to create a query (not a report) that creates a running total based on an order quantity column.

original table: tblOrdSum
InvNum ItemNo  Qty
1001   1234        1                  
1001   1234        1                  
1001   1234        1                  
1001   1234        1                  
1001   5678        1                  
1001   5678        1                  
1001   9123        1                  

new table: tblOrdSum2
InvNum ItemNo  Qty   Total
1001   1234        1        1
1001   1234        1        2
1001   1234        1        3
1001   1234        1        4
1001   5678        1        5
1001   5678        1        6
1001   9123        1        7
0
Comment
Question by:seyfang
6 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 24049312
To create a running sum in a query there has to be a unique key for each record and the records have to be sorted on that key.
The key can be made up of more than one field.

The data example you give does not meet this requirement, so the question is whether there is another field than can be included that would provide the uniqueness requirement.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 24049316
To do this you need a unique identifier for each record, say ID.
0
 
LVL 30

Accepted Solution

by:
hnasr earned 250 total points
ID: 24049356
Added ID and numbered from 1 to 7:

SELECT a.ID, a.invNum, a.itemNo, a.Qty, (Select Sum(b.Qty) from tblOrdSum  b where b.ID<=a.ID) AS Total
FROM tblOrdSum AS a;
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 65

Expert Comment

by:rockiroads
ID: 24049370
do u have a unique id in this table?
you can do this then

SELECT invnum, itemno, qty, dcount("id","mytable","id<=" & mt.id) as rowcnt
from mytable as mt

assuming id is the unique id in this table

remember to leave table alias (as mt)
0
 

Author Closing Comment

by:seyfang
ID: 31565761
I didn't know if there was a way to without a unique ID. I was trying it with DSUM.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 24049798
seyfang!
"I didn't know if there was a way to without a unique ID. I was trying it with DSUM."
This applies if you want to use a query!
With code using recordset you can do without the unique ID.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question