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

Subquery problem

P: 31
Hi All, I am have some trouble with a parameter query that I am hoping someone can guide me.
I have a Parameter query with a few fields that people can filter with, Such as "Department", Date Range, Shift... This query is meant to sum the number of "Yes" answers as a percentage from a Y/N field. The query works fine and gives me all the filtered records depending on the choices in the query.
Here is my problem. I need to create a report that 1st, will display just the parameters they chose, such as "Accounting", 5/1/09-5/31/09, First Shift... and 2nd, give me a TOTAL Percentage for that whole month. Here is my Query:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCTROW [Hand Hygiene].Department,
  2.                    [Hand Hygiene].Date,
  3.                    [Hand Hygiene].HCWorker,
  4.                    [Hand Hygiene].Shift,
  5.                    Sum([Hand Hygiene].HW_AfterGlove)/Count([Hand Hygiene].Key)*100 AS Percentage,
  6.                    Count(*) AS [Count Of HandWashingAfterGloves],
  7.                    Avg([Percentage]) AS [Avg Of Percentage]
  9. FROM               [Hand Hygiene]
  11. GROUP BY           [Hand Hygiene].Department,
  12.                    [Hand Hygiene].Date,
  13.                    [Hand Hygiene].HCWorker,
  14.                    [Hand Hygiene].Shift
  16. HAVING          ((([Hand Hygiene].Department) Like "*" & [What Department?] & "*"
  17.     Or            ([Hand Hygiene].Department) Is Null)
  18.    AND           (([Hand Hygiene].Date) Between [Start Date] And [End Date]
  19.     Or            ([Hand Hygiene].Date) Is Not Null)
  20.    AND           (([Hand Hygiene].HCWorker) Like "*" & [Health Care Worker] & "*"
  21.     Or            ([Hand Hygiene].HCWorker) Is Null)
  22.    AND           (([Hand Hygiene].Shift) Like "*" & [What Shift?] & "*"
  23.     Or            ([Hand Hygiene].Shift) Is Null));
When I add the line: Avg([Percentage]) AS [Avg Of Percentage] I get an error "Subqueries cannot be used in the expression"

I created a second query and the line works fine, and I get a total from the first query, but my problem there is, Reports only allow 1 query. When I added a subreport, I get the Parameters twice, and then it doesn't really work right.

Any help would greatly be appreciated.
Jun 5 '09 #1
Share this Question
Share on Google+
7 Replies

P: 33
Add the Avg([Percentage]) to the report footer of your report in a textbox. This will solve your problems.

As far as dynamic reports go, I haven't tried it. This article looks like it might be of assistance to you and has the database examples for download at the bottom of the page.

** Link removed as per site rules - Please check **

Jun 5 '09 #2

P: 33
It just occurred to me that I might have misunderstood your question. Do you want to show a Total Average for all Departments on the report, but only list the individual departments that were filtered? If this is the case, create a second query that totals the percent of all departments (regardless of the parameters chosen), and then use a DLOOKUP to grab the Total Percentage from the new query. The other query can operate as you have it, only displaying the parameters chosen, because the total percentage will come from a different query. You can find more on DLOOKUP in the Access Help section. It should be pretty straightforward.

Jun 5 '09 #3

P: 31
I will try the Footer Option, it sounds like it might work.
What I was looking for in the report, I created a Form Based Query, and when a user types in lets say:
Department: Education
Health Care Worker: MD
Shift: First
The query will currently pull up the filtered results with a field called Percentage. There may be 200 records in that query.
Rather than have all 200 records on the Report, I was hoping it would look like this:
Department: Education
Healthcare Worker: MD
Shift: First
Average: 95

The Average being the average of all 200 results.
I hope this clears it up.
Jun 5 '09 #4

P: 33
It sounds like the footer option should work. Use an equals sign before the AVG function in the textbox. If your query is already set to group by the fields you are filtering for, the only thing you are missing is your Average Percentage.
Jun 5 '09 #5

Expert Mod 15k+
P: 31,709
Using the predicate DISTINCTROW is entirely superfluous when the GROUP By clause is used.

Having said that I don't see why it would cause the error you report. See Subqueries in SQL to understand what subqueries are about in SQL. Sometimes items in parentheses can be treated as subqueries, but I don't see anything like that here.

Can you say, did the query run ok without just that extra bit (Avg([Percentage]) AS [Avg Of Percentage])?
Jun 5 '09 #6

P: 31
Yes, the query worked fine without the AVG, once I added it, I got the error.
I am going to try the Report Footer for the grand total, One other issue I am trying to work through, is, I made a form based query and I am not sure how to have the results go to a report.
Jun 6 '09 #7

Expert Mod 15k+
P: 31,709
Try removing the DISTINCTROW predicate of your SELECT clause. It shouldn't be there really anyway.
Set the report's Record Souce property to the name of the query.
Jun 6 '09 #8

Post your reply

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