Solved

Access- running total query

Posted on 2009-04-02
6
1,268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
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

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.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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