Download Intermediate Access_..

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Information Technology Services
MS Access:
Creating
Advanced
Queries
ITS@PennState
224B Computer Building
University Park, PA 16802
[email protected]
http://its.psu.edu/training/
Alternative Format Statement
This publication is available in alternative media upon request.
Statement of Non-Discrimination
The Pennsylvania State University is committed to the policy that all persons shall have equal access to programs,
facilities, admission, and employment without regard to personal characteristics not related to ability, performance, or
qualifications as determined by University policy or by state or federal authorities. The Pennsylvania State University
does not discriminate against any person because of age, ancestry, color, disability or handicap, national origin, race,
religious creed, sex, sexual orientation, or veteran status. Direct all inquires regarding the nondiscrimination policy to
the Affirmative Action Director, The Pennsylvania State University, 328 Boucke Building, University Park, PA
16802-5901; tel. (814) 865-4700; TDD (814)863-1150.
MS Access: Creating Advanced Queries
Page 2
Spring 2003
Table of Contents
Objectives ........................................................................................................2
Summary Functions .........................................................................................3
Concatenation ..................................................................................................6
Find Unmatched Queries .................................................................................7
Crosstab Queries ...........................................................................................10
Using the Crosstab Query Wizard..................................................................11
Parameter Queries.........................................................................................14
Multiple-Criteria Parameter Queries...............................................................15
Wildcards in Parameter Queries ....................................................................17
Using Indexes ................................................................................................18
Multiple-Field Indexes ....................................................................................19
Independent Practice Activity .........................................................................20
Additional Resources .....................................................................................21
Objectives

Use a select query to summarize and group data from different tables

Write a concatenation statement to combine values from different fields

Use the Find Unmatched Query Wizard to locate records that do not match between
tables

Design a crosstab query to summarize grouped data

Create single- and multiple-criteria parameter queries

Learn how to use a wildcard in a parameter query

Create single- and multiple-field indexes to quickly sort and filter data in a table
MS Access: Creating Advanced Queries
Page 2
Spring 2003
Summary Functions
A summary function is used to calculate the total of a grouped value.
Go to the Queries database object button
and double-click on Create query in
Design view.
Add the tables and/or queries you wish
to use for your new query. For this
example we are using Order_details.
Add the fields to the QBE grid, then
go to the View menu and select
Totals. In this example, we are
adding Product_ID in the first field,
then building a formula in the second
field cell using the Expression
Builder.
MS Access: Creating Advanced Queries
Page 3
Spring 2003
The QBE grid will now have a new Total row
where you can group on a field and/or choose a
function.
You can click on the Expression
Builder button on the toolbar to create
your calculation.
In the Expression Builder box, type
Total_discount:
Double-click on the Functions folder in the far left
column
Select Built-In Functions in the far left column
Select SQL Aggregate in the middle column
Double-click on Sum in the far right column
The formula in the Expression Builder
box should look like this.
Delete <<Expr>>
After deleting <<Expr>>, edit the
expression to look like this. The
formula will calculate the sum of
discounts given at the sale of each
product. When completed, click Ok.
MS Access: Creating Advanced Queries
Page 4
Spring 2003
Your QBE grid should now look
like this.
Change the format property for the
Discount formula to Currency by rightclicking on the field cell and choosing
Properties from the menu.
On the Total line under the
Discount formula, choose
Expression from the drop-down
menu.
Save the query.
Run the query by clicking on
the exclamation mark button.
MS Access: Creating Advanced Queries
Page 5
Spring 2003
Your final query results should display the
total discount given for each product.
Concatenation
Concatenation means combining values from different fields into one field. You can use the
Expression Builder to do this if you wish.
Click on the Queries database object button,
then double-click on Create query in Design
view.
Add the tables and/or queries for your new
query using the Show Table window. In
this example, we are using the Employees
table.
Add your fields to the QBE grid.
In this example, we added
Employee_ID and Title.
MS Access: Creating Advanced Queries
Page 6
Spring 2003
Click in the third field cell, then click on
the Expression Builder button in the
toolbar.
In the Expression Builder box, type the
following to concatenate the first and last
name fields:
Name: [Employees]![First_name] +
(‘ ‘) + [Employees]![Last_name]
Click Ok. Save the query. Run the
query by clicking on the exclamation
mark.
Observe the query results.
The first and last name fields
should be concatenated or
combined into one field.
Find Unmatched Queries
You use a find unmatched query to view records that don’t have a matching record in another
table.
MS Access: Creating Advanced Queries
Page 7
Spring 2003
Click on the New action button in the database
window toolbar.
Select Find Unmatched Query Wizard and
click Ok.
Confirm that Tables is selected under the
View section of the window.
Choose the table you wish to use. In this
example, we are using Customers.
Click Next.
Confirm the Tables button is checked under the
View section. Select the table you wish to
compare to check for any unmatched records.
Click Next.
MS Access: Creating Advanced Queries
Page 8
Spring 2003
Examine the window. Confirm that the
field in the left window matches up with a
similar field in the right window.
In this example, verify that Customer_ID is
the matching field.
Click Next.
Select the fields you wish to view. Move them
to the Selected Fields window on the right.
Click Next.
Type in a name for the query.
Decide if you wish to view the query results or
modify in Design view.
Click Finish.
View the query results.
MS Access: Creating Advanced Queries
Page 9
Spring 2003
Crosstab Queries
Crosstab queries allow you to display a summary of values based on two different types of
information. Crosstab queries are generally used to display large amounts of data in a
compact format for easy review. The results resemble a spreadsheet format. Most
frequently, crosstab queries are used to calculate averages, sums, or other totals on records,
then group the results into two types of data. One group runs down the left side of the
datasheet while the other runs across the top. See the example below.
In Design view, a crosstab query looks like this.
Row Headings
MS Access: Creating Advanced Queries
Column Headings
Page 10
Summarized Field
Spring 2003
Using the Crosstab Query Wizard
Click the Queries database object button.
Click on the New action in the database
window toolbar.
Select Crosstab Query Wizard and
click Ok.
Under the View section, check the appropriate
radio button for Tables, Queries, or Both. In
this example, check Queries.
Select the table or query you wish to use from
the list. In this example, select
Customers_discounts.
Click Next.
MS Access: Creating Advanced Queries
Page 11
Spring 2003
Choose the field(s) you wish to use as row
headings.
Select them in the order you want the
information sorted.
Look in the lower section to see a sample of
what it would look like.
In this example, we are using Product_name.
Click Next.
Choose the field(s) you wish to use as
column headings.
Look in the lower section to see a sample of
what it would look like.
In this example, we are using
Customer_name.
Click Next.
Next, select the number(field) you want
calculated. In this example, we are using
Discount.
Select the function you would like to
perform. In this example, we are using Avg.
Check the box for Yes, include row sums.
Look at the lower section to see a sample of
what it would look like.
Click Next.
MS Access: Creating Advanced Queries
Page 12
Spring 2003
Name your crosstab query.
Decide if you want to view the query results
or modify in Design view.
Click Finish.
Observe the query results. The
products appear as row headings, the
customer names appear as column
headings, and the average discount
given to each customer on the sale of a
particular product.
Switch to Design view.
In Design view, right-click on the Discount field to
open the Properties window.
From the Format list, choose Currency.
From the Decimal Places list, choose 2.
Close the Properties window. Update the query.
Switch to Datasheet view to see your changes.
MS Access: Creating Advanced Queries
Page 13
Spring 2003
Parameter Queries
A parameter query displays results based on criteria you specify each time you run the query.
Click on the Queries database object button.
Double-click on Create query in Design
view.
Add the tables and/or queries you wish to use
for the parameter query.
In this example, we are using the Products
table.
In this example, type >[Price is
greater than] in the second
Criteria cell under Unit_price,
then save the query.
Run the query by clicking
on the exclamation mark.
MS Access: Creating Advanced Queries
Page 14
Spring 2003
When you run the query, a parameter value
dialog box appears.
Type in the criteria you wish to query.
In this example, enter 1.5, then click Ok.
View the results. All the unit
prices greater than 1.5 should
be displayed. Try running the
query again with a different
criteria.
Multiple-Criteria Parameter Queries
A multiple-criteria parameter query prompts you to enter more than one value to display the
query results.
Click on the Queries database object button.
Double-click on Create query in Design view.
Add the tables and/or queries you wish to use for
the parameter query. In this example, we are
using the Order_details and
Order_shipping_dates tables.
MS Access: Creating Advanced Queries
Page 15
Spring 2003
In the QBE grid, add the fields for your
parameter query. In this example, add
Order_ID, Customer_ID, and
Order_date from the
Order_shipping_dates table and add
Product_ID from the Order_details
table.
To search between a range of dates, type the
following:
Between [Start date] and [End date]
In this example, set this criteria for the Order_date
field. Save the query and run it.
A parameter value dialog box will appear for the
first parameter. In this example, type 12-1-2000
for the Start date. Click Ok.
A parameter value dialog box will now appear for
the second parameter. In this example, type 1210-2000 for the End date. Click Ok.
View the query results. Try
running the query again with
different parameters.
MS Access: Creating Advanced Queries
Page 16
Spring 2003
Wildcards in Parameter Queries
Click on the Queries database object
button, then double-click on Create
queries in Design view.
Add the tables and/or queries from
the Show Table window. In this
example, we will add Order_details
and Order_shipping_dates.
Add the fields for your queries. In this
example, we used the asterisk (*) to
add all the fields from the
Order_details table and the
Order_date field from the
Order_shipping_dates table.
For the criteria under the Order_date
field, type Like [For the month] & “**”
Save and run.
MS Access: Creating Advanced Queries
Page 17
Spring 2003
In the parameter value dialog box, type the
criteria for the query.
In this example, enter 10.
Click Ok.
The query results should show the
order details for all products
ordered in October.
To run the query without specifying a parameter, leave the parameter value dialog box blank
and click Ok.
Using Indexes
Indexes are used to quickly sort and find data in tables. An index performs a search based on
key values. Primary key values are automatically indexed, but you can create indexes for
other values. It is similar to the index in a book which helps you locate information easily.
Indexes can be single-field or multiple-field.
Click on the Tables database object button, then open the
table you wish to index in Design view. In this example,
open Order_shipping_dates by selecting it, then clicking on
the Design action button in the database window toolbar.
MS Access: Creating Advanced Queries
Page 18
Spring 2003
Go to the View menu and choose Indexes.
Enter an Index Name and the field in
which it is located. In this example,
enter Orderdate as the Index Name
and choose Order_date from the Field
Name drop-down list. Choose
ascending for the Sort Order.
Save the table changes and close.
Multiple-Field Indexes
You can create multiple-field indexes based on two or more fields in a table.
Open the table in Design view. In
this example, we will open the
Employees table in Design view.
Then click the indexes button
Enter an index name and the
corresponding field names and sort
orders. In this example, enter
Employee_name, then choose
Last_name and First_name for the
Field Names and Ascending as the
Sort Order.
MS Access: Creating Advanced Queries
Page 19
Spring 2003
Independent Practice Activity
Open Advanced_queries
Create a query that displays the Retailer_code and the total Sale_value for each Retailer from
the Transaction and the Product tables. Create a sum of the Sale_values and group the
query based on the Retailer_codes
Compare the result of the query that you created in Step 2 to the diagram below.
Create a query called Retailer_details that displays the Retailer_code, Retailer_name, and
Contact_name columns. The Contact_name column should contain the values from the
Contact_first_name and Contact_last_name fields of the Retailer table
Create a crosstab query from the Transaction table that displays the total quantity of each
product sold to each Retailer
Create a parameter query from the Transaction table that displays the Product_ID and
Qty_sold fields for all products that have a Qty_sold value that’s less than a specified value
Create a parameter query from the Product table that displays the Product_name,
Qty_available, and Min_stock fields for all products that have the Min_stock value between
two specified values
Close the database
MS Access: Creating Advanced Queries
Page 20
Spring 2003
Additional Resources
MS Access 2002 Help
Help menu (Contents, Answer Wizard, Index tabs)
F1
Ask a Question box
Office Assistant
Books
Special Edition Using Microsoft Access 2002 by Roger Jennings
The Complete Reference: Access 2002 by Virginia Andersen
Mastering Access 2002 by Alan Simpson and Celeste Robinson
Teach Yourself Microsoft Access 2002 Visually by Ruth Maran
Instant Access Databases by Greg Buczek
Microsoft Access 2002 Bible by Cary N. Prague and Michael R. Irwin
Sams Teach Yourself Microsoft Access 2002 in 10 Minutes by Faithe Wempen
Microsoft Access 2002: At A Glance by Perspection
Microsoft Access 2002: Step By Step by Catapult, Inc.
Running Microsoft Access 2002 by John Viescas
Microsoft Pocket Guide to Microsoft Access 2002 by Stephen L. Nelson
Websites
http://its.psu.edu/training/
http://www.fgcu.edu/support/office2000/access/index.html
http://www.cant.ac.uk/title/tutorials/access.htm
http://www.microsoft.com/office/access/default.htm
http://www.microsoft.com/office/access/using/default.htm
http://www.cyber-matrix.com/access.htm
http://www.course.com/downloads/NewPerspectives/office2000/access_main.html
http://www.customguide.com/demos/access2000_2.pdf#page=13
http://databases.about.com/cs/tutorials/
http://ittraining.westmont.edu/access/tutorials.html
http://cisnet.baruch.cuny.edu/holowczak/classes/2200/access/accessall.html
Phone Numbers
Help Desks (215 Computer Bldg 863-2494 and 2 Willard Bldg 863-1035)
Seminar Line 863-9522
MS Access: Creating Advanced Queries
Page 21
Spring 2003
MS Access: Creating Advanced Queries
Page 22
Spring 2003