Solved

calculate the running sum in a query using DSum

Posted on 2006-10-24
11
2,802 Views
Last Modified: 2012-08-13
I am trying to calculate the running sum of the number of tasks overdue.

I have a query (qryTasksOverDue) that joins some tables and calculates whether the task is overdue
(isOverDue is a field that equals 1 or 0)

Sample data from this is (filtered where isOverDue is 1)

DateA            isOverDue      
-----                            ---------
9/3/2006                         1      
9/3/2006                         1      
9/10/2006                         1      
10/15/2006         1      
10/15/2006         1      
10/15/2006         1      
10/15/2006         1      

Here is the query I am having problems with

Based on query above (qryTasksOverDue)

StartDate: DateA (Group by)
SumOfisOverDue: isOverDue (sum)
RunningSum : DSum("isOrigOverDue","qryTasksOverDue",[DateA]<=[StartDate]) (expression)


I get:

StartDate      SumOfisOverDue      RunningSum      (SHOULD BE)
---------      --------------      ----------                      -----------
7/2/2006           0            7            (0)
7/9/2006           0            7            (0)            
7/16/2006           0            7            (0)
7/23/2006           0            7            (0)
7/30/2006           0            7            (0)
8/6/2006           0            7            (0)
8/13/2006           0            7            (0)
8/20/2006           0            7            (0)
8/27/2006           0            7            (0)
9/3/2006           2            7            (2)
9/10/2006           1            7            (3)
9/17/2006           0            7            (3)
9/24/2006           0            7            (3)
10/1/2006           0            7            (3)
10/8/2006           0            7            (3)
10/15/2006    4            7            (7)



I have tried many other ways of using DSum but I have not been able to get anything to work. Anyone know how I can get what I want?

Thanks!
0
Comment
Question by:tammieR
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17798064
two things:
1. Are you going to be displaying this data in a report? if so, just use the running sum property in the report instead of doing it in SQL.
2. can you post your SQL instead of the design view of the query
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 17798124
Hi tammieR,

Modify qryTasksOverDue to aggregate on the date, and then run a query like this:

SELECT q1.DateA, Sum(q1.isOverDue) AS OverdueOnDate,
    (SELECT Sum(q2.isOverDue) FROM qryTasksOverDue AS q2 WHERE q2.DateA <= q1.DateA) AS RunningTot
FROM qryTasksOverDue AS q1
ORDER BY q1.DateA

Regards,

Patrick
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17798154
I think that your column RunningSum Should look like this, you might not have copied it right though:

RunningSum : DSum("isOrigOverDue","qryTasksOverDue","[DateA]<=" & [StartDate])
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17798211
If that does not work, try:

RunningSum : DSum("isOrigOverDue","qryTasksOverDue","[DateA]<=#" & [StartDate] & "#")
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17798224
good point, I saw the missing quotes, but didn't even think, you have to have the number symbols(#) good pickup matthew
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17798312
It may not be relevant :)
0
 
LVL 57
ID: 17798325
Tammie,

  Just a comment; it's been asked where you plan to use this.  If in a form or report, there are better ways then using a sub select or DSum() in the query.  By doing that, you are in affect running a sum query for every record that gets returned.

  For a few records, this won't matter much, but if it's a larger recordset, performance will be poor.

JimD
0
 

Author Comment

by:tammieR
ID: 17798369
yhwhlivesinme:
1. I am planning on using this information in a pivot chart form eventually so I can not just show it in a report. This does work very nicely if I wanted a report.

I also tried putting the quotes on the dsum function like you suggested and it did not give me any results at all. RunningSum was blank.
0
 

Author Comment

by:tammieR
ID: 17798376
Matthewspatrick:
I tried this query and it is giving me exactly what I wanted! THANKS!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17798486
Glad to help :)
0
 

Expert Comment

by:matthewpreston
ID: 25203623
In the accepted solution, how do you "Modify qryTasksOverDue to aggregate on the date"? I am trying to adapt this solution to my query, but I can't figure out what matthewspatrick meant.

Thanks for the help.

Best, Matt
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

919 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

19 Experts available now in Live!

Get 1:1 Help Now