Solved

Access- running total query

Posted on 2009-04-02
6
1,263 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
Comment Utility
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
Comment Utility
To do this you need a unique identifier for each record, say ID.
0
 
LVL 30

Accepted Solution

by:
hnasr earned 250 total points
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now