By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,619 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,619 IT Pros & Developers. It's quick & easy.

Trying to calculate on query to pull a report

P: 22
Hello All, I have been trying to work this out myself based on a response that I received yesterday which worked however I think I am lost in the process. I have a form which I have 6 calculated fields plus information fields like date, type etc. However, I now am gravitating to using the Query for these fields to calculate instead of calculating on the form. So simply the fields below are what I have calculated on the form;

Form Field 1. Name GST =[cost per unit]*[Qty]*0.1
Form Field 2. Name GST Per Item = [GST]/[Qty]
Form Field 3. Name Freight Cost per item =[freight cost]/[Total GST incl]
Form Field 4. Name Total cost order bf GST =[cost per unit]*[qty]
Form Field 5. Name Total GST incl = [total cost oder bf GST&freight]+[GST]
Form Field 6. Name Total Freight+GST =[freight cost] + [Total GST incl]

Now these work on the form but not on the query. modelled from an answer i received yesterday for another query
Expand|Select|Wrap|Line Numbers
  1. SELECT Products.Type, Sum([Qty]-[Qty Out]AS EXPr1
  2. FROM Products
  3. GROUP BY Products.Type;
I tried replicating the concept in the Sql section of the query using the above criteria 1-6 however of course I am receiving all sorts of error messages.

So my mission is to be able to have all 6 calculations and other categories working on the query so that I can pull a report on ALL fields. If someone could give me a head start I would greatly appreciate
Jun 12 '10 #1

✓ answered by MMcCarthy

OK You've done really well, just one more rule to learn. You can't use an alias name for a field in a calculation.

So ...

Expand|Select|Wrap|Line Numbers
  1. Sum([Total Cost of Order before Freight]+[GST]+[Freight Cost]) AS [TOTAL GST incl], 

Expand|Select|Wrap|Line Numbers
  1. Sum(([Cost per unit]*[Qty])+[GST]+[Freight Cost]) AS [TOTAL GST incl], 
the second one gets even more compicated :)

One suggestion I would make would be to build one query with the Total Cost of Order before Freight and the Total GST incl calculated. Then build another query using that query as the base instead of the product table.

Share this Question
Share on Google+
13 Replies

Expert 100+
P: 1,248
It sounds like you are being a little too ambitious with jumping in the editing the sql directly. Not too ambitious, but too quick. Take it slow, it took me a very long time to reach the point where I could code the sql commands without botching the job.

Let Access code the sql for you ... take that sql command you typed above and look at it in the Access query editor. You'll see there how you can accomplish the same thing without typing all that sql code. You'll see one column looks like this: Expr1:[Qty]-[QtyOut].

You'll also see the Grouping row is visible, and this query is grouped by the "Type" column. You turn that grouping on and off by right-clicking in the bottom half of the query editor; that's where you can select Sums, Averages, Min, Max and other options that come with Grouping.

Go back and forth between the SQL code view and the GUI Query editor (by right-clicking in the top half of the query editor). Make a change in one place and go see how that changed the appearance in the other. You'll learn a lot about SQL like that.

I think that you'll see that adding all 6 of those calculations to your query will really be a breeze. Not at first, but soon. Try it.

Jun 12 '10 #2

P: 22
Hi Jim, I understand that I am in the thick of it and jumpig in at the deep end..I have been doing what you suggested now for the better part of 6 hours and I am clearly not understanding what Im doing. It appears info is just now floating around in my head. I have in the SQL of the query the following;

Expand|Select|Wrap|Line Numbers
  1. SELECT Products.ID, Products.[Date Recvd], Products.[Invoice NO], Products.Suppliers, Products.[Product Name], Products.Type, Products.Size, Products.[Load Index], Products.[Cost Per Unit], Products.QTY, Sum([Cost per unit]*[Qty]) AS [Total Cost of Order before GST and Freight], Sum([Cost Per Unit]*[Qty]*0.1) AS GST, Sum([GST]/[Qty]) AS [gst per item], Sum([Total Cost of Order before GST and Freight]+[GST]) AS [TOTAL GST incl], Products.[Freight Cost], Sum([Freight Cost]/[Total GST Incl]) AS [Freight Cost Per Item], Sum([Freight Cost]+[Total GST incl]) AS [GRAND TOTAL Freight + GST incl], Products.[Freight Company], Products.Comments, Products.[Date Outgoing], Products.[Type Out], Products.[Qty Out], Products.Freight, Products.[Freight Send Cost], Sum([Qty]-[Qty Out]) AS [Remaining Stock]
  2. FROM Products
  3. GROUP BY Products.ID, Products.[Date Recvd], Products.[Invoice NO], Products.Suppliers, Products.[Product Name], Products.Type, Products.Size, Products.[Load Index], Products.[Cost Per Unit], Products.QTY, Products.[Freight Cost], Products.[Freight Company], Products.Comments, Products.[Date Outgoing], Products.[Type Out], Products.[Qty Out], Products.Freight, Products.[Freight Send Cost];
I dont know if this makes any sense and is quite a lot of information (set up in the Query design) I apologise but if it makes any sense to yourself or anyone on where it is I am going wrong I would be greatful for help in putting it right

kind regards
Jun 12 '10 #3

Expert Mod 10K+
P: 14,534
Hi Deb

Aggregate functions can get complicated, I'll have to put some rules together one of these days for an Insight when I get the time.

There are a few basic rules that you have to keep in mind.
  • Any field in the SELECT that is not aggregated (summed, counted, etc.) must be included in the Group By clause.
  • All aggregated fields will grouped by all non aggregated fields. By this I mean if I run the following query ..

    Expand|Select|Wrap|Line Numbers
    1. SELECT SalaryScale, Department, Count(EmpName)
    2. FROM EmpTable
    3. GROUP BY SalaryScale, Department
    This will not give me the number of employees on a particular salary scale and the number of employees in each department. What it will give me is the number of employees on a particular salary scale in each department.
  • Use of the HAVING clause can be a difficult concept to get but it's similar to the WHERE clause except it works on the grouped by fields rather than the SELECT fields.

Now looking at your query code you seem to have added all the fields from the SELECT that are not aggregated to the GROUP BY clause so that's good. However, my suspicion is that you have too many fields displaying. If you run a query ...

Expand|Select|Wrap|Line Numbers
  1. SELECT Products.ID, Products.Type, Sum(Products.Qty)
  2. FROM Products
  3. GROUP BY Products.ID, Products.Type
It will just return the quantity for each product id regardless of type. You need to remove product id from the select and group by to get the qty per type. Does this make sense.

You said you were getting errors but I don't see anything obvious to cause the sql not to actually run. I suspect your problems are with the various aggregates. My suggestion would be to use a test query to check each of them out individually and see if they throw an error. It can be a lot easier to manage than a large query like you have.
Jun 12 '10 #4

Expert 100+
P: 1,248
Anybody who spent 6 hours trying to work through it themselves needs and deserves some extra help :)

Mary is right, work through some small tests, try to get your query right without any of the Summed fields and then add 1 field at a time. By switching the view back and forth between Design View and SQL View you will learn a ton about how the SQL code is done.

All that aside, I have something concrete for you. You have mistake in your formatting of a name you chose for one of your Sums.
Sum([Freight Cost]+[Total GST incl]) AS [GRAND TOTAL [Freight + GST incl] is an illegal field name. You cannot have "+" in the name you are giving that sum. My personal habit is to avoid long names and avoid including spaces in the name. It just gets me into trouble eventually. If I want a space in a name for readability, I use an underline. Otherwise I avoid special characters completely.

Jun 12 '10 #5

Expert 100+
P: 1,248
To be more clear, in your code:
AS [GRAND TOTAL Freight + GST incl]
should be
AS [GRAND TOTAL Freight plus GST incl]
or in my view
Jun 12 '10 #6

Expert Mod 10K+
P: 14,534
Good catch Jim. I missed that one :)
Jun 12 '10 #7

P: 22
Hello again, Im starting to feel really dumb and I think my hubby is going to throw me out. I have taken all your suggestions above and have succeded in having the calculations show on the report from the query all but two. I am receving the error message Subqueries cannot be used in the expression <expression>. (Error 3203) I think im starting to understand what this is about but I have now been flipping about the search to no avail I have been trying various ways to sort this issue out...

the SQL that I have in the query is as follows;
PS( I apologies for the huge amount of info)
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. Products.[Date Recvd], 
  3. Products.[Invoice NO], 
  4. Products.Suppliers, 
  5. Products.[Product Name], 
  6. Products.Type, Products.Size, 
  7. Products.[Load Index], 
  8. Products.[Cost Per Unit], 
  9. Products.QTY, 
  10. Sum([Cost per unit]*[Qty]) AS [Total Cost of Order before GST and Freight], 
  11. Sum([Cost Per Unit]*[Qty]*0.1) AS GST, ([GST]/[Qty]) AS [gst per item], 
  12. Sum([Total Cost of Order before Freight]+[GST]+[Freight Cost]) AS [TOTAL GST incl], 
  13. Products.[Freight Cost], 
  14. Sum([Freight Cost]/[QTY]) AS [Freight Cost Per Item], 
  15. Sum([Freight Cost]+[Total GST incl]) AS [GRAND TOTAL Freight plus GST incl],
  16. Products.[Freight Company], 
  17. Products.Comments, 
  18. Products.[Date Outgoing], 
  19. Products.[Type Out], 
  20. Products.[Qty Out], Products.Freight, 
  21. Products.[Freight Send Cost], 
  22. Sum([Qty]-[Qty Out]) AS [Remaining Stock]
  24. FROM Products
  25. GROUP BY
  26. Products.[Date Recvd],
  27.  Products.[Invoice NO], 
  28. Products.Suppliers, 
  29. Products.[Product Name], 
  30. Products.Type, 
  31. Products.Size, 
  32. Products.[Load Index], 
  33. Products.[Cost Per Unit], 
  34. Products.QTY, 
  35. Products.[Freight Cost], 
  36. Products.[Freight Company], 
  37. Products.Comments, 
  38. Products.[Date Outgoing], 
  39. Products.[Type Out], 
  40. Products.[Qty Out], 
  41. Products.Freight, 
  42. Products.[Freight Send Cost], 
  43. Products.[Freight Cost Per Item];
The two pieces of info I have bolded are what I am getting the error messages on. If you would be so kind as to advise me if I am on the right track I would appreciate...
kind regards
Jun 13 '10 #8

Expert Mod 10K+
P: 14,534
OK You've done really well, just one more rule to learn. You can't use an alias name for a field in a calculation.

So ...

Expand|Select|Wrap|Line Numbers
  1. Sum([Total Cost of Order before Freight]+[GST]+[Freight Cost]) AS [TOTAL GST incl], 

Expand|Select|Wrap|Line Numbers
  1. Sum(([Cost per unit]*[Qty])+[GST]+[Freight Cost]) AS [TOTAL GST incl], 
the second one gets even more compicated :)

One suggestion I would make would be to build one query with the Total Cost of Order before Freight and the Total GST incl calculated. Then build another query using that query as the base instead of the product table.
Jun 13 '10 #9

Expert 100+
P: 1,248
I've been programming for 30+ years and I still get that "dumb" feeling. There's always some new language, hardware, database, platform of some kind or a combination of all of the above. Winsock is kickin' my butt right now.

You just have to build an immunity to it, it's a mirage. You're not dumb.

Jun 13 '10 #10

P: 22
Oh by George I think she's got it.....See me doing a little dance...Where do I send the flowers and chocolates?...thank you so much for all your support and patience...I really appreciate your sharing your knowledge and I hope that I am able to also do this one day...I made a new query and used the second code as suggested from msquared as my grand totals...again thank you thank you thank you...mwahhh
Jun 14 '10 #11

Expert Mod 10K+
P: 14,534
OK if you run and save this query as qryFreight1 ...

Expand|Select|Wrap|Line Numbers
  1. SELECT  
  2. Products.[Date Recvd],  
  3. Products.[Invoice NO],  
  4. Products.Suppliers,  
  5. Products.[Product Name],  
  6. Products.Type, 
  7. Products.Size,  
  8. Products.[Load Index],  
  9. Products.[Cost Per Unit],  
  10. Products.QTY,  
  11. Sum([Cost per unit]*[Qty]) AS [TotalCost],  
  12. Sum([Cost Per Unit]*[Qty]*0.1) AS GST, 
  13. Products.[Freight Cost],  
  14. Sum([Freight Cost]/[QTY]) AS [FreightCostPerItem],  
  15. Products.[Freight Company],  
  16. Products.Comments,  
  17. Products.[Date Outgoing],  
  18. Products.[Type Out],  
  19. Products.[Qty Out], 
  20. Products.Freight,  
  21. Products.[Freight Send Cost],  
  22. Sum([Qty]-[Qty Out]) AS [Remaining Stock] 
  23. FROM Products 
  24. GROUP BY 
  25. Products.[Date Recvd], 
  26. Products.[Invoice NO],  
  27. Products.Suppliers,  
  28. Products.[Product Name],  
  29. Products.Type,  
  30. Products.Size,  
  31. Products.[Load Index],  
  32. Products.[Cost Per Unit],  
  33. Products.QTY,  
  34. Products.[Freight Cost],  
  35. Products.[Freight Company],  
  36. Products.Comments,  
  37. Products.[Date Outgoing],  
  38. Products.[Type Out],  
  39. Products.[Qty Out],  
  40. Products.Freight,  
  41. Products.[Freight Send Cost]; 
Then run this query ...

Expand|Select|Wrap|Line Numbers
  1. SELECT  
  2. qryFreight1.[Date Recvd],  
  3. qryFreight1.[Invoice NO],  
  4. qryFreight1.Suppliers,  
  5. qryFreight1.[Product Name],  
  6. qryFreight1.Type, 
  7. qryFreight1.Size,  
  8. qryFreight1.[Load Index],  
  9. qryFreight1.[Cost Per Unit],  
  10. qryFreight1.QTY,  
  11. qryFreight1.[TotalCost] AS [Total Cost of Order before GST and Freight],  
  12. qryFreight1.GST, 
  13. ([GST]/[Qty]) AS [gst per item],
  14. Sum([TotalCost]+[GST]+[Freight Cost]) AS [TOTAL GST incl],  
  15. qryFreight1.[Freight Cost],  
  16. qryFreight1.[Freight Cost Per Item],  
  17. Sum([Freight Cost]+[TotalCost]+[GST]+[Freight Cost]) AS [GRAND TOTAL Freight plus GST incl], 
  18. qryFreight1.[Freight Company],  
  19. qryFreight1.Comments,  
  20. qryFreight1.[Date Outgoing],  
  21. qryFreight1.[Type Out],  
  22. qryFreight1.[Qty Out], 
  23. qryFreight1.Freight,  
  24. qryFreight1.[Freight Send Cost],  
  25. qryFreight1.[Remaining Stock] 
  26. FROM Products 
  27. GROUP BY 
  28. qryFreight1.[Date Recvd], 
  29. qryFreight1.[Invoice NO],  
  30. qryFreight1.Suppliers,  
  31. qryFreight1.[Product Name],  
  32. qryFreight1.Type,  
  33. qryFreight1.Size,  
  34. qryFreight1.[Load Index],  
  35. qryFreight1.[Cost Per Unit],  
  36. qryFreight1.QTY,
  37. qryFreight1.[TotalCost]
  38. qryFreight1.GST,
  39. qryFreight1.[Freight Cost], 
  40. qryFreight1.[Freight Cost Per Item], 
  41. qryFreight1.[Freight Company],  
  42. qryFreight1.Comments,  
  43. qryFreight1.[Date Outgoing],  
  44. qryFreight1.[Type Out],  
  45. qryFreight1.[Qty Out],  
  46. qryFreight1.Freight,  
  47. qryFreight1.[Freight Send Cost],  
  48. qryFreight1.[Remaining Stock];
Jun 14 '10 #12

Expert 100+
P: 1,248
Oh, wow Deb, what a difference a couple of days can make. Congratulations. :)

Jun 14 '10 #13

Expert Mod 10K+
P: 14,534
You put in all the hard work Deb and learned something in the process. That's what it's all about, at least here on Bytes :)
Jun 14 '10 #14

Post your reply

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