Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access- running total query

Posted on 2009-04-02
6
Medium Priority
?
1,273 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 31

Expert Comment

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

Accepted Solution

by:
hnasr earned 750 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 31

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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