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

Is there a way to calculate % in a query?

I have two queries. One is answer my question as to the number of calls and the other one answer to my question number of visits. Both of these queries give me a TOTAL column of the number of calls and visits with respect to the Fiscal Quarters.


Now my question is: Is it possible to create a new query with %? Taking the results from those two queries?
Aug 29 '11 #1
45 2107
Rabbit
12,516 Expert Mod 8TB
Yes. You can join the two queries by the fiscal year and then divide one number by the other for whatever percent you're trying to calculate.
Aug 29 '11 #2
Thanks Rabbit.
I can't seem to join two queries because the query wizard only me to choose One query when creating a new one.
Aug 29 '11 #3
NeoPa
32,556 Expert Mod 16PB
Remember, percentages are stored as simple fractions, so be careful you understand that 50% == 0.5 and 50% =/= 50. The format of the number displayed has no bearing on the actual value itself.
Aug 29 '11 #4
NeoPa
32,556 Expert Mod 16PB
Michelle Copper:
I can't seem to join two queries because the query wizard only me to choose One query when creating a new one.
In the Query menu there is an option to 'Show Tables', from which you can select queries as well as tables.
Aug 29 '11 #5
so you mean I choose the two queries using 'show tables' and add the field that I want to take the average? I tried that but it did not work. Do I simply choose the field I want and add a new column for %?


How do I create % column?
Aug 30 '11 #6
NeoPa
32,556 Expert Mod 16PB
If you want the percentage that field 1 of query A is of field 2 of query B then you can add a field in the new query that is a formula :
Expand|Select|Wrap|Line Numbers
  1. =IIf([Query 2].[Field B] = 0, 0, [Query 1].[Field A] / [Query 2].[Field B])
NB. the IIf() call is there to ensure you never divide by zero.

Also note that this will only show as a percentage if you set the format of the field appropriately. Otherwise it will simply show as a decimal value that is 100th of the value of a percentage (EG. If the result is 34% it will show simply as 0.34 unless the format property is set).
Aug 30 '11 #7
Thanks NeoPa.

Access is giving me error and says it does not recognize the field as a valid field name or expression.

This is what I put under FIELD in new query:

Expand|Select|Wrap|Line Numbers
  1. =IIf([qry3].[TotalOfMetricsID] = 0, 0, [qry4].[ TotalOfMetricsID] / [qry3].[TotalOfMetricsID]) 

From the two queries:

qry3
FY
Quarter
TotalOfMetricsID
Call
Visit



qry4
FY
Quarter
TotalOfMetricsID
Yes
Aug 30 '11 #8
NeoPa
32,556 Expert Mod 16PB
Can you post exactly what it says please Michelle.

Also post the SQL of the query if you can (See below for instructions on how to get this from your query).
To extract the SQL from a QueryDef (saved Access query) it is only necessary to view the query in SQL View. This shows the underlying SQL for the QueryDef and is text (See Access QueryDefs Mis-save Subquery SQL for problems with SubQueries). When a QueryDef is open (either in Design View or Datasheet View) it is possible to switch to the SQL View simply by selecting View \ SQL View from the menu.

From here it is simple to Copy & Paste it to wherever you need it.
Aug 30 '11 #9
sure.

qry3 SQL View says

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count(qryExportMetrics.MetricsID) AS CountOfMetricsID
  2. SELECT qryExportMetrics.FY, qryExportMetrics.Quarter, Count(qryExportMetrics.MetricsID) AS TotalOfMetricsID
  3. FROM qryExportMetrics
  4. WHERE (((qryExportMetrics.Activity)="Call" Or (qryExportMetrics.Activity)="Visit"))
  5. GROUP BY qryExportMetrics.FY, qryExportMetrics.Quarter
  6. ORDER BY qryExportMetrics.FY, qryExportMetrics.Quarter
  7. PIVOT qryExportMetrics.Activity;
  8.  
qry4 SQL View says

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count(qryExportMetrics.MetricsID) AS CountOfMetricsID
  2. SELECT qryExportMetrics.FY, qryExportMetrics.Quarter, Count(qryExportMetrics.MetricsID) AS TotalOfMetricsID
  3. FROM qryExportMetrics
  4. WHERE (((qryExportMetrics.[Would have called 911?])="Yes"))
  5. GROUP BY qryExportMetrics.FY, qryExportMetrics.Quarter
  6. ORDER BY qryExportMetrics.FY, qryExportMetrics.Quarter
  7. PIVOT qryExportMetrics.[Would have called 911?];
  8.  

and as for the error message. It says "The Microsoft Office Access database engine does not recognize '[qry4].[TotalOfMetricsID]' as a valid field name or expression"
Aug 30 '11 #10
NeoPa
32,556 Expert Mod 16PB
I was asking about the SQL for the query that gave you the error message, but by a happy coincidence, the problem is illustrated perfectly by the SQL for [qry4].

You refer to [qry4].[TotalOfMetricsID] (from the error message) yet I suspect [qry4] has no such field. Run qry4 and see what field names are available. I don't use crosstab queries much myself and can't easily test this for myself, so let me know if [TotalOfMetricsID] turns up. I suspect you may need to use [CountOfMetricsID] instead.
Aug 30 '11 #11
I do have TotalOfMetricsID in my qry4

I have attached an image of how my qry 4 looks. qry3 looks similar to that.


the new query SQL view is
Expand|Select|Wrap|Line Numbers
  1. SELECT IIf([qry3].[TotalOfMetricsID]=0,0,[qry4].[ TotalOfMetricsID]/[qry3].[TotalOfMetricsID]) AS Expr1
  2. FROM qry3, qry4;

Attached Images
File Type: jpg qry4.jpg (51.5 KB, 322 views)
Aug 30 '11 #12
Rabbit
12,516 Expert Mod 8TB
You have a space in your [qry4].[ TotalOfMetricsID] qualification.
Aug 30 '11 #13
NeoPa
32,556 Expert Mod 16PB
That suggests the error message posted should have been :
Expand|Select|Wrap|Line Numbers
  1. The Microsoft Office Access database engine does not recognize '[qry4].[ TotalOfMetricsID]' as a valid field name or expression
This would have given us the answer back then. Never mind. Remember though, that in coding, and database coding in particular, attention to detail is critical. It's no accident that the best coders are always highly fussy over details.
Aug 30 '11 #14
I just tried and its giving me some results now. What I don't understand is why is it showing 132 record instead of 12 record? :S

I have attached an image here and I hope someone can figure out where I went wrong.
Thanks

Attached Images
File Type: jpg qry.jpg (114.0 KB, 343 views)
Aug 30 '11 #15
NeoPa
32,556 Expert Mod 16PB
That one's easy from the previous SQL posted. You have the two queries included without any type of JOIN. See SQL JOINs. Pay particular attention to the part about Cartesian Products (which is what you have here).
Aug 30 '11 #16
Rabbit
12,516 Expert Mod 8TB
Just a heads up, you seem to be missing a quarter in one of your queries. Which means you will have to use an outer join of some sort. And you will need to handle the null in your calculation.
Aug 30 '11 #17
I just tried using this
Expand|Select|Wrap|Line Numbers
  1. SELECT Quarter,TotalOfMetricsID,Quarter,TotalOfMetricsID
  2. FROM qry4 LEFT JOIN qry3
  3.   ON qry4.Quarter=qry3.Quarter 
but it is giving me an error (see insert image "errormsg" )

The reason being is because I have the same field in both the queries.
I cannot change the name as they are both from the same table.

I have attached another image of my two queries.

:S

** Edit **


Attached Images
File Type: jpg qry.jpg (56.7 KB, 358 views)
File Type: jpg errormsg.jpg (16.9 KB, 312 views)
Aug 30 '11 #18
Rabbit
12,516 Expert Mod 8TB
In your SELECT clause, you have to tell it which of the two to use.
Aug 30 '11 #19
NeoPa
32,556 Expert Mod 16PB
When any field has an ambiguous source (multiple sources contain the same named field) then the references must be qualified to include the source names :
Expand|Select|Wrap|Line Numbers
  1. SELECT qry3.Quarter
  2.      , qry3.TotalOfMetricsID
  3.      , qry4.Quarter
  4.      , qry4.TotalOfMetricsID
  5. FROM   qry4 LEFT JOIN qry3
  6.   ON   qry4.Quarter=qry3.Quarter
Aug 31 '11 #20
Thanks Rabbit and NeoPa!

I used the codes that were in post#20 and I added Fiscal Year along with it and I added a new column at the end for calculating averages.

it does give the results but however, I would like it to have like to have all the QUARTER 1 to be grouped together to whichever year it belongs.

I have attached an image here to show the output. As you can see, it is very messy and confusing.

what should I do to have them group together?

** Edit **

Attached Images
File Type: jpg Untitled.jpg (56.4 KB, 367 views)
Aug 31 '11 #21
NeoPa
32,556 Expert Mod 16PB
Use a GROUP BY clause :
Expand|Select|Wrap|Line Numbers
  1. SELECT   qry3.Quarter
  2.        , qry3.TotalOfMetricsID
  3.        , qry4.TotalOfMetricsID
  4. FROM     qry4 LEFT JOIN qry3
  5.   ON     qry4.Quarter=qry3.Quarter
  6. GROUP BY qry3.Quarter
Aug 31 '11 #22
NeoPa
32,556 Expert Mod 16PB
Unless your explanation is somewhat misleading and you really want the records sorted by year then quarter (rather than grouped), which would be to use an ORDER BY clause instead :
Expand|Select|Wrap|Line Numbers
  1. SELECT   qry3.FY
  2.        , qry3.Quarter
  3.        , qry3.TotalOfMetricsID
  4.        , qry4.TotalOfMetricsID
  5. FROM     qry4 LEFT JOIN qry3
  6.   ON     qry4.Quarter=qry3.Quarter
  7. ORDER BY qry3.FY
  8.        , qry3.Quarter
This can also be managed in Design View of the query by specifying which fields to sort on in the grid.
Aug 31 '11 #23
Thanks NeoPa !
Post #22 is giving me error. See attachment #post22

Post #23 - I have tried this code but it is still giving me the similar results to what I have done before.

If you see my qry3 and qry4 there is only ONE row with Quarter 1 of 2008 and so on. That is how I would like when I join qry3 and qry4 together.
Sorry for creating all these confusion.

** Edit **


Attached Images
File Type: jpg post#22.jpg (19.0 KB, 328 views)
File Type: jpg post#23.jpg (66.5 KB, 363 views)
Aug 31 '11 #24
NeoPa
32,556 Expert Mod 16PB
You're right about post #22. It's rubbish. I must have sent it in a bit of a rush. Let's focus on post #23. I'll just fix your post to show the image (you can do this too you know) so I can work with it but I'm leaving shortly so I'll have to address it properly later.
Aug 31 '11 #25
Thanks NeoPa! I will wait for your reply
Aug 31 '11 #26
NeoPa
32,556 Expert Mod 16PB
Michelle:
If you see my qry3 and qry4 there is only ONE row with Quarter 1 of 2008 and so on. That is how I would like when I join qry3 and qry4 together.
I'm looking at the results of your new query and I don't understand how the quoted statement makes sense (as there are clearly multiple records with the same [Quarter] and [FY] values).

If you want multiple records grouped together then you have to choose how those items that aren't part of the grouping (The two [TotalOfMetricsID] fields) should be shown or aggregated (Only grouped fields are guaranteed to be the same across all records within a particular group, so you must choose a way to show the other data. An example would be to show the Sum of their values. Another to show the Max of them).
Sep 1 '11 #27
Sorry NeoPa. I was actually referring to my original qry3 and qry 4 (before they were join together).

As for using Max , I tried and it shows what I wanted but only the my 4th column seems weird. I have attached an image here again to show what I mean. The whole column is now "299".
And this is my SQL code
Expand|Select|Wrap|Line Numbers
  1. SELECT qry3.FY, qry3.Quarter, Max(qry3.TotalOfMetricsID) AS MaxOfTotalOfMetricsID, Max(qry4.TotalOfMetricsID) AS MaxOfTotalOfMetricsID1
  2. FROM qry3, qry4
  3. GROUP BY qry3.FY, qry3.Quarter;

Attached Images
File Type: jpg groupby.jpg (51.9 KB, 336 views)
Sep 1 '11 #28
how do I make my image viewable?
Sep 1 '11 #29
Rabbit
12,516 Expert Mod 8TB
In post 23, you forgot to join FY to FY.
Sep 1 '11 #30
Thanks Rabbit..
I just tried but it did not work.
Do I simply add another line to the code? Like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT   qry3.FY
  2.        , qry3.Quarter
  3.        , qry3.TotalOfMetricsID
  4.        , qry4.TotalOfMetricsID
  5. FROM     qry4 LEFT JOIN qry3
  6.   ON     qry4.Quarter=qry3.Quarter
  7.   ON    qry4.FY=qry3.FY
  8. ORDER BY qry3.FY
  9.        , qry3.Quarter
  10.  

the error message says "Syntax error (missing operator) in query expression 'qry.Quarter=qry3.Quarter' ON qry4.FY=qry3.F'
Sep 1 '11 #31
I tried this as well. Still not working

Expand|Select|Wrap|Line Numbers
  1. SELECT   qry3.FY
  2.        , qry3.Quarter
  3.        , qry3.TotalOfMetricsID
  4.        , qry4.TotalOfMetricsID
  5. FROM     qry4 LEFT JOIN qry3
  6.   ON     qry4.Quarter=qry3.Quarter,
  7. qry4.FY=qry3.FY
  8. ORDER BY qry3.FY
  9.        , qry3.Quarter
  10.  
  11.  
Sep 1 '11 #32
Rabbit
12,516 Expert Mod 8TB
Change the second ON to an AND.
Sep 1 '11 #33
AWESOME!!! IT FINALLY WORKED!!!! PHEW!!

Thanks Rabbit and Thanks NeoPa! You guys are great!
Sep 1 '11 #34
Okay.. I am facing problem again. so I renamed my queries to the names that I have wanted following the codes from above and somehow, very strangely, it does not work :S

Now where did I go wrong? I simply replaced the name using the same codes

qry3 = casesReceiving
qry4 = casesSeen

Expand|Select|Wrap|Line Numbers
  1. SELECT   casesReceiving.FY
  2.        , casesReceiving.Quarter
  3.        , casesReceiving.TotalOfMetricsID
  4.        , casesSeen.TotalOfMetricsID
  5.  
  6. FROM     casesSeen LEFT JOIN casesReceiving
  7.   ON     casesSeen.Quarter=casesReceiving.Quarter,
  8. and casesSeen.FY=casesReceiving.FY
  9. ORDER BY casesReceiving.FY
  10.        , casesReceiving.Quarter
  11.  

The error message says "JOIN expression not supported"
Sep 1 '11 #35
Rabbit
12,516 Expert Mod 8TB
You have a comma at the end of line 7.
Sep 1 '11 #36
Thanks.
I just removed the comma and changed them to
Expand|Select|Wrap|Line Numbers
  1. SELECT   casesReceiving.FY
  2.        , casesReceiving.Quarter
  3.        , casesReceiving.TotalOfMetricsID
  4.        , casesSeen.TotalOfMetricsID
  5.  
  6. FROM     casesSeen LEFT JOIN casesReceiving
  7.   ON     casesSeen.Quarter=casesReceiving.Quarter
  8. and casesSeen.FY=casesReceiving.FY
  9. ORDER BY casesReceiving.FY
  10.        , casesReceiving.Quarter
  11.  
  12.  
now it is saying that "The microsoft office Access database engine does not recognize 'casesReceiving.TotalOfMetricsID' as a valid field and name or expression"
Sep 1 '11 #37
okay..Never mind.
I found the problem!
Sorry~~
Thanks!
Sep 1 '11 #38
Now I can SMILE again!
Sep 1 '11 #39
Rabbit
12,516 Expert Mod 8TB
I'm glad you got it worked out. Good luck!
Sep 1 '11 #40
NeoPa
32,556 Expert Mod 16PB
Michelle:
How do I make my image viewable?
Much to catch up on in this thread. You guys have been busy while I was away busy (as it were).

Use the [ IMGNOTHUMB ] tags. I've added spaces in there to illustrate (as it doesn't work when speces are included but it does show what I mean (See BB Code for more on available BB Code tags).

[ IMGNOTHUMB ]http://bytes.com/attachments/attachment/5378d1314881693/groupby.jpg[ /IMGNOTHUMB ]

Getting the link to enter is easy when the attachment is posted. Simply right-click on the link and select Copy Link Location.
Sep 1 '11 #41
NeoPa
32,556 Expert Mod 16PB
It seems (now I've caught up with the thread fully) that you now have it working as an ordered list, but the grouping is no longer used. Is that because you decided it was unnecessary after all, or because it proved too difficult to manage?

I'm just curious. I can leave alone if you're happy, but if you wanted to progress it to handle that I'm sure we could help further.
Sep 1 '11 #42
I do not know myself either.:D I just kept following what Rabbit suggested and I was managed to get the right query.


This is how it looks now.



Thanks again NeoPa!
Attached Images
File Type: jpg Untitled.jpg (13.6 KB, 265 views)
Sep 2 '11 #43
okay.. seems like I did not get the image viewable right *sigh*
Sep 2 '11 #44
Okay. I got the image up and working!
Sep 2 '11 #45
NeoPa
32,556 Expert Mod 16PB
8-) I'm happy if you're happy.
Sep 2 '11 #46

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

Similar topics

4
by: Bacci | last post by:
I have two tables. The first is "Locations" which has 52,000 zip codes w/cooresponding latitudes and longitudes. The second "Suppliers" has 2,000 company names and addresses. The user enters a...
2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
1
by: sg_s123 | last post by:
============================================================================ 02-Feb-04 03-Feb-04 Staff Staff 0800hr- 1300hr- 1700hr- 1900hr- 0800hr- 1300hr- 1700hr- 1900hr- Number...
1
by: Simon Bailey | last post by:
I currently have a query calculating the gap in days between two dates. The fields being "DateLookedAt" and "DateResolved" plus the calculated field "TimeTaken". I am looking to add another...
5
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed...
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
21
by: jennwilson | last post by:
Ok - So, I am back. I would like to count the number of times a specific record appears in a field from one table in my query and then use that value in the same query to calculate an average....
3
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
0
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.