Chapter 4 – Advanced Queries
INTRODUCTION
Let us continue our Query adventure by working on:
- Create more queries with multiple tables of source data
- Perform aggregate functions in a query to tabulate data
Guided Exercises
Go to your IU OneDrive and open your username-Haptown database.
The Relationships diagram is at the end of the instructions, but can be opened from the Database Tools tab in your Access file, too. When reading the directions for a query, you need to decide which table(s) to use and looking at the Relationships can be helpful.
Lookup Field Filtering
One thing we can do is change a lookup field to only show a specific subset of options in a drop-down list. For example, we can change our Ticket Data’s assigned officer to only allow us to select Police Department employees, instead of any employee. So let’s do that.
- Open tblTicketData in Design view.
- Switch to the Lookup tab at the bottom.
- Notice there is a SQL query for Row Source, which selects the ID, last name, and job title from tblEmployee. We can alter that, so click on the three dots … at the end of that Row Source line.
- In this tblTicketData: Query Builder window, add the Department field to our controls, uncheck the Show option, and type “Police Department” (including the quotes) in the Criteria space. Your window should look like this:
- Now click the X to close that Query Builder window, and Yes to the save prompt.
- Switch back to Datasheet view, selecting Yes to save if prompted. Notice how the drop-down options for the officer assignment is limited to just those employees from the Police Department.
- Let’s assign the first 20 tickets to a random selection of police staff so we can query this data later.
- Close and save tblTicketData.
Advanced Queries
-
Did you remember the Totals Row? You have been given the task of following up on mayor feedback that was deemed Negative and had to do with safety (use the word “safe”). Create a query that lists consultant’ FirstName, LastName, and EmailAddress, as well as SubmissionDate. Ensure you filter the Sentiment and Comment fields correctly. Save the query as qryGE6.
- Let’s expand that search for any feedback item that came in on 4/3/2025, as we think a large event may have been going on that day. Let’s also sort Ascending by LastName.
- Run your query and notice consultants show up multiple times, because of different feedback submissions.
- Since we are targeting April 3, we don’t need to list SubmissionDate. So let’s hide that appropriately.
- Let’s also show how many submissions each consultant has assigned herein, and we’ll call that field FeedbackCount. That field should look like: FeedbackCount: [FeedbackID] and have an aggregate of Count.
- Change the sort to Descending by FeedbackCount.
- Now we have a list of the consultants handling these April 3rd feedback submissions and how many they are dealing with. We can contact them and follow-up as needed. Save this query as qryGE7.
- We want to report out on how much capital we have listed in the database. Create a query that will give a AssetTotalValue of all asset current values. Format this field as Currency if necessary. Add a second field called NumAssets that counts all the assets we have. Save this query as qryGE8.
- Now let’s narrow that down to a specific set of Computer assets. Create a query that will list the BuildingName and a calculated field that is the total current value called BldgAssetValue of all assets per department.
- Add a field called ValueChange which will calculate the difference between the current value and the acquisition cost. These will be negative numbers, as assets depreciate in value over time.
- Similar to the last query, add a NumAssets field to calculate the count for each building.
- You may optionally enable the Totals button in the query results and use a sum to verify that grant total here matches the one from qryGE8.
- Finally, filter the results by the AssetType “Computer” and sort Descending by BldgAssetValue. Save this query as qryGE9.
Notice that if we wanted to calculate the AVERAGE asset value per item, we’d have to take those total sums and divide by the counts. But we cannot do that in a single query in Access – that’s where we would use what we call “query on query”, but we will not cover that in this class.
Make sure all objects are closed. Close the database. Double-check the Last Modified date for your database file.
More Practice
Work you should do before our next class
Go to your IU OneDrive and open your username-HomeGoodsStore database.
- Let’s look at our software licensing. Create a query that lists the software vendors and how much money we spend on licenses – specifically for anything that is a subscription. Remember to take into account the number of seats we pay for. This should list Vendor and TotalSftCost, sorting Descending by the latter of these. Save the query as qryMP5.
- Note that you can TEMPORARILY add the CostPerLicense and Quantity fields to your results to check your math. Just don’t leave those fields in the query when you save.
- Let’s look at the software costs per deparment now. Create a query that will list the Department and a calculated field SfwFees for all subscriptions. Save the query as qryMP6.
- Note that you can use the Totals button in your query results for these last two queries – why might the sums be different?
- If each feedback submission takes the consultant an average of three hours to investigate and resolve (or get work started on in the department), how much money will we spend to resolve all feedback submissions based on the consultant hourly rates? Create a query that calculates this field called ConsultantCosts as well as a field named NumSubmissions that gives the number of submissions. Save this query as qryMP7.
- Create a query that lists average, minimum, and maximum employee salaries. Use the fields AvgSalary, MinSalary, and MaxSalary. Format as Currency as needed and save as qryMP8.
- Create a query that will list the LastName and JobTitle from tblEmployee for those officers we assigned to tblTicketData. Also list the earliest date (as OldestTicketDate) of the tickets they are assigned. Sort Ascending by OldestTicketDate and save this query as qryMP9.
- Your results may vary depending on who you assigned to which tickets up in the first part of this chapter.
- Create a query that lists the AverageAge of Haptown’s buildings, as well as the average number of years (as AveYearsSinceReno) since renovation. You may use 2025 instead of calculating a dynamic year of “today”. Save this as qryMP10.
- What is your observation about our overall building status?
Make sure all objects are closed. Close the database. Double-check the Last Modified date for your database file.