473,387 Members | 3,787 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.

Sum() function returning garbage numbers...

Scott Price
1,384 Expert 1GB
Hello Gurus of the Access world...

This one has me stumped. I am developing a meal planning database for large functions (1000 people +- for up to 4 days). I have stored base recipes, as well as stored historic data from past years. To generate a shopping list report I am using this sql query:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblIngredient.ItemName, Sum(tblRecipeIngredient.Quantity) AS TotQuantity, tblMeasureType.MeasureDesc, tblIngredient.RecipeMeasureID, tblConvInformation.ConventionName, tblIngredient.ShopMeasureID, tblHistory.ConvID, tblHistory.Year, Sum(tblRecipe.BaseNbrOfServings) AS SumOfBaseNbrOfServings, Sum(tblHistory.PersonCountEst) AS SumOfPersonCountEst, tblIngredient.IngredientID
  2. FROM (tblConvInformation INNER JOIN (((tblMeal INNER JOIN tblHistory ON tblMeal.MealID = tblHistory.MealID) INNER JOIN tblMealStatus ON tblHistory.MealStatusID = tblMealStatus.MealStatusID) INNER JOIN tblRecipe ON tblHistory.RecipeID = tblRecipe.RecipeID) ON tblConvInformation.ConvID = tblHistory.ConvID) INNER JOIN ((tblMeasureType INNER JOIN tblIngredient ON tblMeasureType.MeasureID = tblIngredient.RecipeMeasureID) INNER JOIN tblRecipeIngredient ON tblIngredient.IngredientID = tblRecipeIngredient.IngredientID) ON tblRecipe.RecipeID = tblRecipeIngredient.RecipeID
  3. WHERE (((tblMealStatus.IncludeOnShopList)=True))
  4. GROUP BY tblIngredient.ItemName, tblMeasureType.MeasureDesc, tblIngredient.RecipeMeasureID, tblConvInformation.ConventionName, tblIngredient.ShopMeasureID, tblHistory.ConvID, tblHistory.Year, tblIngredient.IngredientID, tblIngredient.ItemCategoryID
  5. HAVING (((tblHistory.ConvID)=[forms]![frmConvInformation].[ConvID]) AND ((tblHistory.Year)=Format(Date(),"yyyy")))
  6. ORDER BY tblIngredient.ItemName, tblIngredient.ItemCategoryID;
The query is working fine... That is, it returns numbers without giving any error messages. The problem lies in the two Sum() fields BaseNbrOfServings and PersonCountEst. When I look at the numbers returned from these two fields, and then compare them with a manual calculation of the base tables they refer to, the numbers end up wildly off!

One quick example to illustrate is: Eggs are referred to in 3 recipes; For the year and convention chosen one of these recipes is referred to twice, and the other two once each; The corresponding numbers SHOULD be then BaseNbrOfServings = 575 (twice 250 + 50 + 25) & PersonCountEst = 900 (300 + 300 + 250 + 50) However, the numbers returned are SumOfBaseNbrOfServings 1075, SumOfPersonCountEst 1450.

The total eggs required should come out to be 60 dz * (900/575) = 94 dz... Using the returned numbers from the query results in a shopping list requirement of 81 dz...

I've been losing hair, brain cells and sleep over this one :-) Needless to say, I can't trust any of the numbers returned by this query!

Any help would be appreciated greatly.

Regards,
Scott
Aug 10 '07 #1
8 1961
missinglinq
3,532 Expert 2GB
Scott, I'm going to leave this question for some of our more SQL savvy members, but when posting SQL code, especially as complicated as this, please tag it specifically as SQL code. It's really simple; after hi-liting the code and clicking on the # icon, go to the opening code tag and change code to code=sql. Just like that, with no spaces between code = or sql.

Doing this allows the editor to bold the various key words in the SQL statement, making it much easier for everyone to read! I've gone ahead and re-tagged this one for you.

Thanks!

Linq ;0)>
Aug 10 '07 #2
Scott Price
1,384 Expert 1GB
Scott, I'm going to leave this question for some of our more SQL savvy members, but when posting SQL code, especially as complicated as this, to tag it specifically as SQL code. It's really simple; after hi-liting the code and clicking on the # icon, go to the opening code tag and change code to code=sql. Just like that, with no spaces between code = or sql.

Doing this allows the editor to bold the various key words in the SQL statement, making it much easier for everyone to read!

Thanks!

Linq ;0)>
Thanks, Linq... I saw that you (or someone) had done that for me. I'm aware of doing that (at least for vb code :-) but in the heat of the moment it escaped me.

Thanks again,
Regards,
Scott
Aug 10 '07 #3
missinglinq
3,532 Expert 2GB
Great! As I said, just put sql where you'd put the vb for VBA code.

Linq ;0)>
Aug 10 '07 #4
JKing
1,206 Expert 1GB
Two thoughts off the top of my head.

Is it possible for a convention to be held twice in one year?

Check that your sum is not equal to the total for the itemcategory rather than the specific item.
Aug 11 '07 #5
Scott Price
1,384 Expert 1GB
Thanks, Jared, for looking at this one...

As for:


Is it possible for a convention to be held twice in one year?
No, only one convention will be held per year.

As for:

Check that your sum is not equal to the total for the itemcategory rather than the specific item.
I don't think so... I just did a quick analysis on that, and came up with 4425 or something like that, looking at the personcountest for each recipe that requires the itemcategory Dairy/Egg...

I'm working on a workaround at the moment, because I'm finding that this query still wouldn't come up with the correct amounts even if the Sum() fields DID come out accurately!!

Again, thanks for looking... I'll post back here if/when I get the right #'s.

Regards,
Scott

I WOULD still like to know why it's pulling the wrong numbers in, though...
Aug 11 '07 #6
JKing
1,206 Expert 1GB
Have a look at this join:

tblMeasureType.MeasureID = tblIngredient.RecipeMeasureID

Different names here. Have you just named the foreign key different or is this an incorrect field? Or does RecipeMeasureID the foreign key for tblRecipe?
Aug 11 '07 #7
Scott Price
1,384 Expert 1GB
Have a look at this join:

tblMeasureType.MeasureID = tblIngredient.RecipeMeasureID

Different names here. Have you just named the foreign key different or is this an incorrect field? Or does RecipeMeasureID the foreign key for tblRecipe?
I'll have to investigate that one a little further, but at the moment, I THINK it's just the foreign key named differently. (the different naming is because later I want to implement the ability to convert from the measurement used in recipes to the measurement used in shopping... at this point, though, that isn't implemented).

Regards,
Scott
Aug 11 '07 #8
Scott Price
1,384 Expert 1GB
I'll have to investigate that one a little further, but at the moment, I THINK it's just the foreign key named differently. (the different naming is because later I want to implement the ability to convert from the measurement used in recipes to the measurement used in shopping... at this point, though, that isn't implemented).

Regards,
Scott
The full workaround I managed is too complex to post here, and not really applicable to the original query, but I did manage to finally get the right numbers outputted to the shopping report.

Basically it involved abandoning the query above and starting over from scratch, doing the math of calculating the quantity of each ingredient per recipe BEFORE plugging that into the final shopping list query, then finally summing each already calculated ingredient, passing each revised quantity through a function to convert decimals to fractions (if anyone wants to see the function to convert decimals to fractions, let me know... it's pretty cool, actually) and only then passing the results over to the shopping report.

Thanks, Jared for trying to help on this rat's nest query!

Regards,
Scott

Now off to clean up the redundant and no longer needed queries that I've created and copied in order to try to figure this thing out...
Aug 11 '07 #9

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
6
by: un[real] | last post by:
Like I said in the title, I have to calculate the sum of the numbers of an interger with a recursive fonction. Exemple: n=1255, sum= 1 + 2 + 5 + 5= 13 Do I have to use a string on can I use a...
3
by: papaja | last post by:
Hello, My table has 2 columns with some numbers: col1 col2 --------------- 5 0 0 5 0 10 2 0 8 0
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
2
n8kindt
by: n8kindt | last post by:
i don't know what to do about this one. there are some very strange things going on. i have a continuous form with a bound textbox and a bound toggle button. the textbox is named "Payment" and the...
54
by: bearophileHUGS | last post by:
Empty Python lists don't know the type of the items it will contain, so this sounds strange: 0 Because that may be an empty sequence of someobject: 0 In a statically typed language in...
4
Alireza355
by: Alireza355 | last post by:
I have a table, with 3 columns: column1 column2 column 3 10 some numbers here some text here 11 some numbers...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.