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

Trying to calculate on query to pull a report

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], 
becomes

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.

13 2506
jimatqsi
1,271 Expert 1GB
Heart01,
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.

Jim
Jun 12 '10 #2
heart01
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
deb
Jun 12 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
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
jimatqsi
1,271 Expert 1GB
Deb,
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.

Jim
Jun 12 '10 #5
jimatqsi
1,271 Expert 1GB
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
AS [GTFandGST]
Jun 12 '10 #6
MMcCarthy
14,534 Expert Mod 8TB
@jimatqsi
Good catch Jim. I missed that one :)
Jun 12 '10 #7
heart01
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]
  23.  
  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
Deb
Jun 13 '10 #8
MMcCarthy
14,534 Expert Mod 8TB
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], 
becomes

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
jimatqsi
1,271 Expert 1GB
Deb,
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.

Jim
Jun 13 '10 #10
heart01
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
MMcCarthy
14,534 Expert Mod 8TB
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
jimatqsi
1,271 Expert 1GB
Oh, wow Deb, what a difference a couple of days can make. Congratulations. :)

Jim
Jun 14 '10 #13
MMcCarthy
14,534 Expert Mod 8TB
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

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

Similar topics

6
by: B Love | last post by:
I am wondering if I can have a dynamically generated report based upon the results of a query (in Access2000). Any ideas? Related to that (if that cannot be done) can the output of a query...
2
by: Scott Baird | last post by:
HELP!!! I've got a DB with two tables (relationships with a autonumber). The database works just fine, but I've got a report I simply can't make work. I've made a query that uses data from...
4
by: Richard Hollenbeck | last post by:
The following query takes about one second to execute with less than 1,000 records, but the report that's based on it takes from 15-30 seconds to format and display. That's frustrating for both me...
2
by: amith.srinivas | last post by:
Hi all, From a word macro in VBA, I am trying to create a report in access DB. The report is based on a query with a parameter. I am using Set rpt = Application.CreateReport rpt.RecordSource =...
1
by: Learner | last post by:
Hi there, I have installed Sql server 2005 developer on my machine which already has a Sql server 2000 installed on. Now i am trying to query the Sqlserver 2005 data(Ex: from Person.Address...
6
by: finrsteve | last post by:
Please help, I'm trying to query a list of dates with only 09/01/xx - 09/30/xx. Doesn't that require writing an expression in the criteria field?? If so, what?
2
by: sara | last post by:
I am helping a non-profit track their elder clients and care given to the clients. The organization would like a report that shows various info on ALL clients, such as: # in each town, # and...
1
by: teneesh | last post by:
Okay. . . . I have a query from a view that should pull up multiple records. When I run it in sql server, it pulls up all records. But when I create the report for it, it only pulls up the first...
4
ollyb303
by: ollyb303 | last post by:
Hello, Trying to help a friend/colleague out with a database and we've both drawn a blank. Not even sure if this is possible. The database has a table (Table1) with a several columns: ID,...
4
mb60
by: mb60 | last post by:
I generated a query based report from access table.I would like to save it in the table for future reference. Reason: Generated report is a mcq question paper of a chapter. If I would like to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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?
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...

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.