Solved

Access- running total query

Posted on 2009-04-02
6
1,266 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

856 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