473,387 Members | 1,942 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,387 software developers and data experts.

Problem with the aggregate Sum function

Hi all! I am trying to have a query return the sum of hours in a certain field. My problem is that when I use the Total Sum, the sum does not add up to the real sum. I can't seem to find out why. I know the query is pulling data from a numeric field and that when I don't Sum it return the correct records. Another peice of information is that some of the numbers are negative, but for some records it sums right and for others it does not, but it does not give me any error massages. I am using Access 2000. Here is the SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT [9A FML hours].ID, [9A FML hours].[DeptID Short], [9A FML hours].TRC, Sum([9A FML hours].Quantity) AS SumOfQuantity, [9A FML hours].[Hrs Yr], [FLSA status].[Sal Plan]
  2. FROM [9A FML hours] LEFT JOIN [FLSA status] ON [9A FML hours].ID = [FLSA status].ID
  3. GROUP BY [9A FML hours].ID, [9A FML hours].[DeptID Short], [9A FML hours].TRC, [9A FML hours].[Hrs Yr], [FLSA status].[Sal Plan]
  4. HAVING ((([9A FML hours].TRC)="FEX") AND ((Sum([9A FML hours].Quantity))>0))
  5. ORDER BY [9A FML hours].[Hrs Yr];
Thank you for your help!!!!!
Sep 5 '08 #1
4 8352
Stewart Ross
2,545 Expert Mod 2GB
Hi, and welcome to Bytes!

It isn't at all clear from what you've posted above whether the groupings in your query are correct for the totals you require - you don't really tell us anything at all about the data or what you mean when you say the totals aren't correct.

The totals calculated will apply to the smallest grouping of distinct rows in your query which satisfy your criteria in the HAVING clause. As you will see from the SQL, the grouping you are using is by ID, [DeptID Short], TRC, [Hrs Yr], and [Sal Plan] (whatever they are). Without knowing more about your data we cannot really advise you further.

It would help to see a sample of your data, and a sample of what you expect to see from such a total. That way we can judge whether your query is suitable or not for doing so.

-Stewart
Sep 5 '08 #2
I am not sure if this will give more information to my problem, but here are a couple rows of my data:
Expand|Select|Wrap|Line Numbers
  1. . ID    TRC  Quantity  Hrs Yr
  2. 103187  FEX     -8      2007
  3. 103187  FEX      8      2007
  4. 600508  FEX      5      2007
  5. 600508  FEX      6      2007
  6. 601729  FEX     -8      2007
  7. 601729  FEX      8      2007
But when I do Sum for the quantity field, the sums are not always correct. Here is what it shows after I do Sum:
Expand|Select|Wrap|Line Numbers
  1. . ID    TRC  SumOfQuantity  Hrs Yr
  2. 103187  FEX       16        2007
  3. 600508  FEX       16        2007
  4. 601729  FEX        0        2007
Is this easier to understand my problem?

Thank you
Sep 5 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Ok, this does at least give some indication of your data; but there is no mention of the Sal Plan field which is part of your Group By clause. Are you really posting the data that your query is operating on? I'll explain why I am not at all sure you are below.

For ID 103187 there are only two rows listed and these would sum to 0 unless there is something else we are not seeing - and your sum shows the value 16, which cannot be obtained by summing the two rows listed. Don't assume that Sum is wrong - it is correctly summing what it has been told to sum. Since this does not match the data posted, what you have posted is clearly not what is being summed. In other words, the data actually being totalled is not the same as you think it is - and you are not going to resolve this until you look at the actual rows the totals query is operating on. It is also a sample of these rows which you should post here to help us help you.

It is possible that the underlying query may have incorrect joins somewhere, either in the current query or in any underlying one that we can't see, leading to multiple rows being returned for each ID. If this happens your totals are bound to be incorrect. To test this I would save a copy of your query under a new name, turn totalling off altogether, remove the computed total field, then run the query and check the actual rows it is operating upon - as these are the ones which will be summed when you turn totalling back on.

The skill in resolving this kind of problem is not to make incorrect assumptions about your data without fully testing your assumptions for validity. You need to look at the actual data the query is operating on, not a sample from an underlying table or another query. Incorrect assumptions leads to incorrect diagnoses leads to no solution to your problem.

-Stewart
Sep 5 '08 #4
Wow! It worked! I still don't know what it is summing, but now I understand that it is totalling more records than what my query returns. I was able to create another query of my original query and total only the results I want. Thanks, that answers my question.
p.s. I took a couple fields off because I thought they were unecessary.
Thank you again!
Sep 5 '08 #5

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

Similar topics

1
by: Saurabh Aggrawal | last post by:
Hi, On line nos. 24, 25, 26 24: wstring cfMethods = {{L"setLabel"},{L""}}; 25: wstring cfProperties= {{L"isVisible"},{L""}}; 26: wstring cfEvents =...
2
by: jc | last post by:
Hi. Just as we have AVG(COLUMN_NAME) and MAX(COLUMN_NAME) how can I write my own variation of a such a function. If I can appreciate how to do this, then I hopefully I can write a...
2
by: Claudio Lapidus | last post by:
Hello I would like to know how can I define/create a new aggregate function. I need a custom function that operate on a set of text strings and return a certain string aggregate based on certain...
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
8
by: Floortje | last post by:
Hi i have been struggeling with this question for quite some time now. I have some helper classes that handle images (upload an image, create thumbnails and show a imagelist), links (add link,...
4
by: shapper | last post by:
Hello, I have the following Linq query: var q = (from p in database.Posts join pt in database.PostsTags on p.PostID equals pt.PostID join t in database.Tags on pt.TagID equals t.TagID group...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...

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.