Creating complex queries using nesting

Slides:



Advertisements
Similar presentations
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Advertisements

Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
Access Lesson 2 Creating a Database
Querying a Database Using the Select Query Window
Creating a Blank Database 1. Open up Microsoft Access 2. Click on Blank document button 3. On the right panel, Specify the location for saving your database.
Chapter 2 Querying a Database
Mary K. Olson PS Reporting Instance – Query Tool 101.
Microsoft Access 2010 Chapter 7 Using SQL.
Access Tutorial 3 Maintaining and Querying a Database
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 3 – Finding, Filtering,
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
MS Access: Database Concepts Instructor: Vicki Weidler.
Datasheets II: Sum, sort, filter, and find your data Overview: Sum it up, and more Access 2007 makes it easier than ever to perform math functions on your.
Advanced Tables Lesson 9. Objectives Creating a Custom Table When a table template doesn’t suit your needs, you can create a custom table in Design view.
1 Access Lesson 3 Creating Queries Microsoft Office 2010 Introductory Pasewark & Pasewark.
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 1Advanced Tables, Relationships, Queries, and Forms Chapter 3Advanced Query Techniques.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Microsoft ® Office Access ® 2007 Training Datasheets II: Sum, sort, filter, and find your data ICT Staff Development presents:
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
Microsoft Access 2010 Building and Using Queries.
Analyzing Data For Effective Decision Making Chapter 3.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 6 – Designing.
Lesson 2.  To help ensure accurate data, rules that check entries against specified values can be applied to a field. A validation rule is applied to.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
SESSION 3.1 This section covers using the query window in design view to create a query and sorting & filtering data while in a datasheet view. Microsoft.
CSC 240 (Blum)1 Introduction to Data Entry, Queries and Reports.
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T7 PROBLEM SOLVING USING ACCESS.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T7: Problem Solving Using Access 2007 Business Driven Technology.
What are queries? Queries are a way of searching for and compiling data from one or more tables. Running a query is like asking a detailed question of.
XP. Objectives Sort data and filter data Summarize an Excel table Insert subtotals into a range of data Outline buttons to show or hide details Create.
You can sort Access data so you can view records in the order you want to view them, and you can filter data so you only see the records you want to see.
1 Lesson 18 Managing and Reporting Database Information Computer Literacy BASICS: A Comprehensive Guide to IC 3, 3 rd Edition Morrison / Wells.
Lesson 7 – Microsoft Excel 2010 Working with Tables, PivotTables, and PivotCharts.
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
Lesson 4.  After a table has been created, you may need to modify it. You can make many changes to a table—or other database object—using its property.
Excel part 5 Working with Excel Tables, PivotTables, and PivotCharts.
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 2 ® Building Queries.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T7: Problem Solving Using Access 2007 Business Driven Technology.
Microsoft ® Excel ® 2013 Enhanced Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts.
Reports. Reports display information retrieved from a database in an attractive printed format. Reports can be created directly from tables, but More.
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
Lesson 23 Managing and Reporting Database Information
Queries.
Access Chapter 2 Querying a Database.
Plug-In T7: Problem Solving Using Access 2007
Access Maintaining and Querying a Database
Database Queries.
Creating and Modifying Queries
Microsoft Official Academic Course, Access 2016
Microsoft Official Academic Course, Access 2016
Creating Noninput Items
M1G Introduction to Database Development
Topic 15 Lesson 1 – Action queries
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
Topic 12 Lesson 2 – Retrieving Data with Queries
Shelly Cashman: Microsoft Access 2016
Unit J: Creating a Database
Assignment resource Working with Excel Tables, PivotTables, and Pivot Charts Fairhurst pp The commands on these slides work with the Week 2 Excel.
Presentation transcript:

Creating complex queries using nesting Topic 14 Lesson 1 – Subqueries or Nested queries

Nested Queries Some complex queries must be based on the results of other queries. The result of a query is a virtual table, i.e., something that looks and can be used like a table, but is not actually stored in the database as a table. A query can be used (as a table is used) in the design of a query.

Subqueries A subquery is a query statement that’s nested inside of another query. Sometimes need to use the results of a query as a field in another query, or as a criterion for a query field.  Example: How many orders have a total under $2,000? To find the answer, first need to calculate order totals and filter out those that are $2,000 and under. Then do a count.

The Database Layout These are all of the tables in the database:

Example Query I How many orders were placed that had a total of less than $2000?

Step 1: Total for Each Order? Design View Datasheet View Using the Principle of Simplest Query we break this query into two subqueries: Widening, then Aggregation.

Step 2: Filter Orders Less Than $2,000? Design View Datasheet View The Principle of Simplest Query would break this query into three subqueries (widening, followed by aggregation, followed by selection). A more descriptive alias for OrderTotals is FilteredOrderTotals.

Step 3: Count the Resulting Rows Build a new query with the previous query as a subquery Design View Datasheet View

Example Query II Who placed these orders less than $2,000? Use OrdersTotal again as a subquery Combine with Orders and Contacts to get customer information Use Group By to remove duplicates There are 15 contacts in total

Watch Out: Access Caches Queries Whenever Access executes a query it saves (“caches”) the result. When that query is used as a subquery, Access uses the cached result instead of running it again. If you then update the subquery, Access does not automatically refresh the result for the query that uses the subquery.

Refreshing Queries To refresh all queries, you need to: Close all queries Click the right mouse button on the query tab and select “Close” Load the query again double-click on the query in the navigator This forces a refresh.

Use Step by Step Decomposition How many orders are there that have a total value of less than $1000? Create a query that finds all orders with a value less than $1000 Save the query under an appropriate name Create another query based on the previous query that COUNTs all of the rows CAUTION: Do not modify or rename queries that are used as subqueries.

Solution with Subqueries Subquery: OrdersLessThan1000 Note the use of the subquery and the aggregate COUNT function OrderLessThan1000 is a convenient shortcut, but violates WASE. We combine Widening, Aggregation and Selection in one query. So there are 21 orders with a total less than $1000

Example Query III What is the average cost of all orders? Note the use of the subquery and the aggregate AVG function Subquery: OrderTotals OrderTotals violates WASE. Exercise: rewrite using two queries: a Widening and an Aggregation subquery.

Example Query IV How many different people bought each product? Attempted query

Example: Counting Unique Occurrences How many different people bought each product? Attempted query Count of Contact IDs

But Grouping by Contact IDs shows 2 different contacts ordered Product #8

Why? Let’s remove the Totals and Ungroup.

Why? Let’s remove the Totals. Someone ordered it twice We have duplicates Group by removes duplicates but doesn’t count. Count aggregates but doesn’t remove duplicates. So what do we do?

Solution Use subquery to perform grouping ProductContactNoDups

Solution Use subquery to perform grouping, then count

TRY FOR YOURSELF… Access Queries

Question 1 Which contacts placed three or more orders?

Question 2 How many contacts placed three or more orders?

Question 3 Which states had contacts that placed 3 or more orders?

Maximum and Minimum What is the maximum amount of any order that was ever placed? What is the minimum amount of any order that was ever placed? Which order was the minimum order? Who placed the minimum (smallest) order?

Maximum Query What is the maximum amount of any order that was ever placed?

Maximum Query What is the maximum amount of any order that was ever placed? Use OrdersTotal again as a subquery Apply the MAX aggregate function

Minimum Query What is the minimum amount of any order that was ever placed?

Query What is the minimum amount of any order that was ever placed? Use OrdersTotal again as a subquery Apply the MIN aggregate function

Minimum Order Query Which order was the minimum order?

Minimum Order Query Which order was the minimum order? Requires the previous query and OrdersTotal as subqueries Can use one of two approaches Approach 1: Use Criteria Approach 2: Create Relationship

Query Who placed the smallest order?

Query Who placed the smallest order? Use previous query as subquery Combine with Orders and Contacts table

Try this: Which orders contained a line item for the least expensive product (based on CurrentUnitPrice)? Who ordered the least expensive product?

Parameterized Query To allow user input for a query value: specify a variable that has a name different from any of the field names Since the value for the field is unknown, the database queries the user for its value

Hiding Subqueries Queries (and tables) can be “hidden”: Right-click on query in navigation panel Select “Hide in this Group”

Unhiding Queries Click anywhere in the Query Explorer Select “Navigation Options…” Check “Show Hidden Objects” Now all hidden queries are visible and can be unhidden.

Summary Group By removes duplicate rows where the Group By values are the same Aggregate functions apply to groups or entire tables depending how they are used Subqueries are necessary to build complex queries