Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query total returns much higher value than expect in Access 2010

Posted on 2014-10-28
17
Medium Priority
?
150 Views
Last Modified: 2014-10-30
I have a simple query that correctly returns three records as shown below when no Group By is used.  It simply lists each record separately.

Amt      NbrAffordableUnitsCreated      NbrJobsCreated
$70,000.00      140      
$589.06            10
$1,074,047.00            80

When I add a Group By to sum the Amt, NbrAffordableUnitsCreated, and NbrJobsCreated columns the Amt appears as $1,444,638,537 instead of $1,144,636 while the other two colums sum correctly.  Something is causing the Amt to increase roughly a 1,000 times more than it should.  A copy of the query and output is attached (you may need to enlarge to read it).

Any suggestions on why this is happening would be greatly appreciated.
0
Comment
Question by:Liberty4all
  • 8
  • 6
  • 3
17 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40409666
No attachment
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40409679
Any join involved in this query, since that can be one of the reasons for odd sum?
0
 

Author Comment

by:Liberty4all
ID: 40409744
Sorry, I forgot to click Upload File.  There is no join in the query.
SampleQuery.pdf
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40409752
Can you post the SQL query from the SQL view? it will be more clear , at least to me :)
0
 

Author Comment

by:Liberty4all
ID: 40409756
SELECT Sum([main table].[Dollar Amount]) AS Amt, Sum([main table].NbrAffordableUnitsCreated) AS NbrAffordableUnitsCreated, Sum([main table].NbrJobsCreated) AS NbrJobsCreated
FROM [main table]
WHERE ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) OR ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes))
GROUP BY [main table].Category
HAVING (((Sum([main table].NbrAffordableUnitsCreated)) Is Not Null) AND (([main table].Category)="Investments")) OR (((Sum([main table].NbrJobsCreated)) Is Not Null) AND (([main table].Category)="Investments"));
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40410086
It is most likely rounding errors adding up.
I guess your Dollar Amount field is of data type Double or Single and not Currency.

Try this modification:

SELECT Sum(CCur([main table].[Dollar Amount])) AS Amt

and watch the three records. Then sum these.

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40410994
I modified the SQL statement as shown below.  When running it produces "Invalid use of Null".  If I remove the Group By it won't run because the NbrAffordableUnitsCreated field is not part of an aggregate function.  I'm sure the same applies to the field NbrJobsCreated.

Please tell me how to modify it more.  The format of the Dollar Amount field in the table is currency.  

SELECT Sum(CCur([main table].[Dollar Amount])) AS Amt, Sum([main table].NbrAffordableUnitsCreated) AS NbrAffordableUnitsCreated, Sum([main table].NbrJobsCreated) AS NbrJobsCreated
FROM [main table]
WHERE ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) OR ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes))
GROUP BY [main table].Category
HAVING (((Sum([main table].NbrAffordableUnitsCreated)) Is Not Null) AND (([main table].Category)="Investments")) OR (((Sum([main table].NbrJobsCreated)) Is Not Null) AND (([main table].Category)="Investments"));
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40411083
The format of the field has no importance, only the data type.
If it contains Nulls, modify like this:

SELECT Sum(CCur(Nz([main table].[Dollar Amount],0))) AS Amt

Or how about just filtering out the Nulls:

SELECT
    Sum(CCur([main table].[Dollar Amount])) AS Amt,
    Sum([main table].NbrAffordableUnitsCreated) AS NbrAffordableUnitsCreated,
    Sum([main table].NbrJobsCreated) AS NbrJobsCreated
FROM
    [main table]
WHERE
((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) OR ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) And [main table].NbrAffordableUnitsCreated Is Not Null
GROUP BY
    [main table].Category
HAVING
    Sum([main table].NbrJobsCreated) Is Not Null AND [main table].Category="Investments";

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40411243
The Dollar Amount field has no null values for records where Category = Investments.

When using the following SELECT statement the dollar amount still shows as $1,444,638,537, or a 1,000 times more than it should.  The other two fields sum correctly.  Can you think of something else I should try, or look at?

SELECT
    Sum(CCur([main table].[Dollar Amount])) AS Amt,
    Sum([main table].NbrAffordableUnitsCreated) AS NbrAffordableUnitsCreated,
    Sum([main table].NbrJobsCreated) AS NbrJobsCreated
FROM
    [main table]
WHERE
((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) OR ((([main table].[Activity Date])>=#1/1/2014# And ([main table].[Activity Date])<#9/30/2014#) AND (([main table].Approved)=Yes)) And [main table].NbrAffordableUnitsCreated Is Not Null
GROUP BY
    [main table].Category
HAVING
    Sum([main table].NbrJobsCreated) Is Not Null AND [main table].Category="Investments";
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40411376
How many records holds "main table"?

What are the results if you remove the criteria?

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40411807
There are 87,709 records in the main table divided among three categories.  The records go back to 1998.  If I filter only on investments with activity dates from 1/1/2014 to 9/30/2014 I get 3300 records which, in Excel, still sum as the queries above.  If I remove the filters for NbrAffordableUnitsCreated and NbrJobsCreated the dollar amount is the same as above.  This suggests the filters for NbrAffordableUnitsCreated and NbrJobsCreated are being ignored.  Only three records meet the selection criteria as originally written.  

If I add a filter for NbrAffordableUnitsCreated (Is not Null) the result is correct (returns one record).  If I replace the filter for NbrAffordableUnitsCreated with one for NbrJobsCreated (Is not Null) the result is also correct (returns two records).  

The problem may be with the logic used to filter for NbrAffordableUnitsCreated and NbrJobsCreated.  The filters as orginally written visually make sense but are not producing the correct result.  Both of these fields are full of null values except for the three records returned.  Can you suggest another way to write the filters that would fix this?
0
 
LVL 52

Accepted Solution

by:
Gustav Brock earned 1500 total points
ID: 40411940
Then I guess you not filter for Sum Is Null but on the individual records.
Move it to the Where clause:

(([main table].NbrAffordableUnitsCreated Is Not Null) AND ([main table].Category="Investments"))
OR
(([main table].NbrJobsCreated Is Not Null) AND ([main table].Category="Investments"));

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40412122
I will run it without trying to sum results.  Your logic looks fine but for some reason I cannot get it to work.  If the number of such records increases signficantly I can always export the results to Excel and sum results there.

I appreciate your help.
0
 

Author Closing Comment

by:Liberty4all
ID: 40412418
Even though I cannot get the logic to work as desired the concept as described by Gustav in his last posting is the solution.  I will run separate queries to get what is needed.  Instead of using

 (([main table].NbrAffordableUnitsCreated Is Not Null) AND ([main table].Category="Investments"))
OR
(([main table].NbrJobsCreated Is Not Null) AND ([main table].Category="Investments"));

I will use something like

(([main table].NbrAffordableUnitsCreated Is Not Null) AND ([main table].Category="Investments"));

in one query and the following in the second.

(([main table].NbrJobsCreated Is Not Null) AND ([main table].Category="Investments"));
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40412626
If both NbrAffordableUnitsCreated and NbrJobsCreated can be Null at the same time, that could explain the difference.

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40413689
For the three records that meet the selection criteria that is not the case, but it is for all others.  In theory I could make the filter something like NbrAffordableUnitsCreated Is Not Null AND NbrJobsCreated Is Null on one line, and NbrAffordableUnitsCreated Is Null AND NbrJobsCreated Is Not Null on the second line as this matches the records as they currently exist.  However, this approach returns no records.
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 40413712
OK, then I would leave it with the method that works.

/gustav
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

824 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