473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Running Sum in Query (DSum)

94
I have a query pulling information from multiple tables, one of which [tblDistLearnTrack] contains a list of students with an auto number [dist_auto_num] their assessment information and how long the assessment took in minutes [distduration].

My query is quite complex, using many tables and containing various parameters based on another form to show a list of assessments specific to a student.

I am trying to achieve a running total of [distduration] alongside the assessment length in my query and am having some difficulty. I realise i have to use DSum but can't quite get it to work.

I have pulled into my query all of the field names mentioned above and currently have the following entered as an expression in my query:
Expand|Select|Wrap|Line Numbers
  1. Running Total: DSum("[distduration]","tblDistLearnTrack","[dist_auto_num] <= [dist_auto_num]")
At the moment when running the query my 'Running Total' column contains no data. Where am i going wrong????
May 30 '08 #1
7 7488
Stewart Ross
2,545 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. Running Total: DSum("[distduration]","tblDistLearnTrack","[dist_auto_num] <= [dist_auto_num]")
At the moment when running the query my 'Running Total' column contains no data. Where am i going wrong????
Well, the WHERE part of the DSUM can't discriminate the right rows, as you are comparing a field to itself within the expression.

[dist_auto_num]<=[dist_auto_num]

At best, this will always evaluate as True (because a field is always less than or equal to itself in value) in which case you should have had a total returned - wouldn't have been the right total, but a total nonetheless. At worst, if the DSUM could not evaluate the expression as valid, you would get no total returned. I wonder what has happened in your case? It could also be that DSUM cannot compute a total for the distduration field for some other reason I can't guess at.

Anyway, you will need to supply DSum with the value of the current autonumber for it to work. Assuming that the autonumber field is the same name, there is just a minor adjustment to make:
Expand|Select|Wrap|Line Numbers
  1. Running Total: DSum("[distduration]","tblDistLearnTrack","[dist_auto_num] = " & [dist_auto_num])
I have the nagging feeling that the DSum approach may not work well in this case, but without further details on what the underlying queries are I can only point out the flaw in its original form and leave it to you to test that any totals returned in due course are accurate.

-Stewart
May 30 '08 #2
Lewe22
94
Stewart,

I tried your formula but all it did was show the same value that is displayed in [tblDistLearnTrack].[distduration]. Any other ideas?

The way of my thinking is that every time a record is created in [tblDistLearnTrack] an auto number is assigned, increasing every time.
Therefore i need a way of totaling the [distduration] for all records with a smaller auto number.

Also, i am wondering if the DSum will need to include the same parameters as my query. Does it work independently from the query result set?
May 30 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi lewe22. The result you found with DSUM is what I was referring to when saying I doubted the use of DSUM in this case. It seemed to me that you were unclear what kind of running SUM you wanted.

I suggest that you consider in more detail what you are trying to achieve; I am pretty sure it is not a running total of the assessment durations, at least not in the way you have tried to do so up to now.

If I read your first post correctly, you have a single row for each student, identified by your autonumber field. You have a nominal and actual assessment duration recorded for each of those rows. If you just sum based on the autonumber as the row selector you will indeed just return the current value of the duration, as you have found, as you are summing the one row only.

What is it you are trying to sum? Is there more than one row for each student, which there needs to be if you are to use SUM at all? Do you really need a running sum of the duration? If you have more than one row, what then discriminates one student from the next? (Whatever it is, it is this you would need to use in the DSUM, not the autonumber.)

I would be glad to assist you, but I think you need to be clear what it is you are trying to achieve and I am unable to glean this from your posts so far.

-Stewart

ps Ahh, you have added to your post - let me comment to see if I can assist.
May 30 '08 #4
PianoMan64
374 Expert 256MB
Stewart,

I tried your formula but all it did was show the same value that is displayed in [tblDistLearnTrack].[distduration]. Any other ideas?

The way of my thinking is that every time a record is created in [tblDistLearnTrack] an auto number is assigned, increasing every time.
Therefore i need a way of totaling the [distduration] for all records with a smaller auto number.

Also, i am wondering if the DSum will need to include the same parameters as my query. Does it work independently from the query result set?
Lewe22,

Your going to have to post what querys and table structure you have in order to be able to give you a specific answer. From what I've read so far, it looks like you don't have a start or a stop time post anywhere in your table structure, and you're going to have to have that in order to compute the difference in time between two fields.

Most of what you're trying to do can be done with a Group query so that you can group together what values you need and it will make them unique for each record returned.

Let's see what you have, and I can give you a more detailed answer.

Thanks,

Joe P.
May 30 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Yep, I go with Joe P. You can't use the autonumber alone to do the sum - otherwise how do you know where one student's record changes to the next? You are missing the student reference from your table - which is the true key for the student, not the autonumber field.

-Stewart
May 30 '08 #6
Lewe22
94
tblDistLearnTrack contains a number of fields but for the purpose of what i'm trying to achieve it contains [dist_student_ref], [c_session], [c_code], [dist_auto_num], [distduration].

dist_student_ref, c_session and c_code are passed to the query from the previous form which restricts the query to 1 student for 1 course for 1 session (year)
Jun 2 '08 #7
NeoPa
32,556 Expert Mod 16PB
A sideways view :
It is almost never appropriate to use running sums in a query.

Whenever you think one is required, it is almost certain that another approach makes more sense, one that you are simply not seeing.

Totals are possible, across groups.
Records in a query should rarely be cosidered as ordered data. Think of them as unique sets of data which all, individually, match a certain pattern.

Where running sums may sensibly be required, is in a report. Reports DO support this concept.
Jun 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: tukaram.thatikonda | last post by:
Hi Guys, I have written a small windows application in VB.Net to test ADO.Net performance while executing long running query. The query works most of the time but fails sometime. I am trying...
1
by: ian.davies52 | last post by:
I'm having a problem running a query. I get the "too many fields" error message, but I only have 162 fields in the query and I thought the limit was 255. The problem query (Query1) is based on...
4
by: Kory | last post by:
How do you stop a long running query with a SQLConnection or SQLCommand? Calling Close on either just waits until the query is done. Is there a way to stop it? The following on a long query ...
6
by: johntarr | last post by:
I hope I am not asking about something that has been done before, but I have searched and cannot find an answer. What I am trying to do is to run a query, and then perform some logic on the...
8
by: John | last post by:
Hi I am using the below code to run a sql on an underlying access table; insStr = "INSERT INTO ( Action, , , Request_Date ) " & _ "VALUES (""Modify Client"", 93, ""Administrator"", Now())"...
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
0
by: Prabhakar78 | last post by:
How to tune long running query?Please any one can provide me the steps to tune long running query in DB2 V9.1.0 and the environment is AIX (OS)
2
by: hafner | last post by:
Hello all, I have an extremely simple query I'm running on a linked table. However, when I run it (primarily, I'm exporting to a .txt file, but the behavior persists even if I run the query), it...
6
by: sarah2855 | last post by:
Hello everyone, I'm running a Make-Table Query in Access. They was working fine up intill an hour ago and suddenly now that I'm running the query , I keep getting this error message: Can not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.