Sie sind auf Seite 1von 47

Access 2003: Step Five – Advanced Queries The University of Akron

TABLE OF CONTENTS

CLASS OVERVIEW ........................................................................................................ 2


LESSON 1: OPERATORS............................................................................................... 3
LESSON 2: CONCATENATING FIELDS .................................................................... 4
LESSON 3: USING CALCULATIONS IN A QUERY............................................... 11
EXERCISE 1: WORKING WITH CALCULATIONS IN A QUERY ............................................ 17
LESSON 4: CREATING TOTALS QUERIES............................................................ 21
EXERCISE 2: TOTALS QUERY WITH GROUPING - USE THE BETWEEN OPERATOR ........... 25
LESSON 5: CROSSTAB QUERY – ANOTHER TYPE OF TOTALS QUERY ..... 27
LESSON 6: TYPES OF JOINS ..................................................................................... 31
PART A: INNER JOINS ..................................................................................................... 32
PART B: LEFT OUTER JOINS ........................................................................................... 35
PART C: RIGHT OUTER JOINS ......................................................................................... 37
LESSON 7: USING THE AND OPERATOR .............................................................. 39
LESSON 8: USING THE OR OPERATOR................................................................. 42
LESSON 9: USING THE AND AND OR OPERATORS TOGETHER ................... 44
APPENDIX A: SELECTING THE HIGHEST VALUES .......................................... 45
APPENDIX B: UPDATING/EDITING DATA IN A QUERY DYNASET............... 47

These materials were developed and are owned by The University of Akron.
All rights reserved.
©2005 The University of Akron

Page 1
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Class Overview
In this fifth step in learning to use Microsoft Access, you will expand your knowledge of
queries. You will learn how to:
¾ Concatenate fields (combine fields of the same field type so that the contents of
the fields are displayed in one column, such as LastName, FirstName)
¾ Calculate in a query
¾ Use inner and outer joins
¾ Create a Totals query
¾ Create a special type of Totals query called the cross-tab query (summary data in
a spreadsheet-like format)
¾ Use the operators AND and OR to create criteria

To have the best experience in this class, it is recommended that you have completed
Access Steps one through four, or have equivalent knowledge and experience. It is
assumed that you have the following skills:
¾ Knowledge of common database terms
¾ Creation of a new database
¾ Design and creation of related tables
¾ Creation of basic forms and reports, using the wizards
¾ Navigation in a table, form, or query design
¾ Ability to sort, find, and filter data in a table or form
¾ Ability to enter and edit data in a table, dynaset, or form
¾ Creation and modification of select, update, make table, parameter, and append
queries
¾ Ability to export data to Excel
¾ Ability to import data from Excel

Page 2
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 1: Operators
Operators are symbols and words that specify the action to take with your data. You use
them to write expressions for criteria and calculations in queries or to calculate in forms
and reports.

The basic types of operators are:1

Comparison: Use these operators to measure data in your database against specific
values or values in other fields.
Examples: < 100 or >=#01/01/05# or >[ArrivalDate]

Arithmetic: Use these operators to perform the math such as addition, subtraction,
division or multiplication. Example: [Pay Rate]*1.02

Logical: Use these operators to apply logic that determines whether conditions are
true or false. Frequently used logical operators are AND, OR, NOT, and
BETWEEN. Example: Dept equal to SA AND Pay Rate > 25.00

1
Microsoft Office Online web-based training. Queries II: Limit your data and calculate values.

Page 3
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 2: Concatenating Fields


In a query, you may select related data that is stored in two separate fields of the tables.
In the results, the data will appear in two separate columns. Some examples are last
names and first names of individuals, or cities and states of residence. In the query
results, you may prefer that the last name and first name be displayed in one column or
that the city and state be displayed in one column. This is done by using concatenation.
Concatenation combines fields of the same data type to display the contents of the fields
in one column of the dynaset.

What you do What happens


1. Start Access by clicking on the Start The application window for Access is
button. opened with the task pane displayed.

Choose Programs, Microsoft Office,


Microsoft Office Access 2003.

2. In the task pane, click on the Open or The security message is displayed.
link.
3. In the Open dialog box, double click
on the Access Class Files folder to
open it.
4. Double click on the company.mdb
file.

Page 4
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens

5. Click on . The task pane is closed. The company


database is opened. The company database
window is displayed.

6. In the database window, click on the The list of queries is displayed.


object button.

7. In the database window, click on the The New Query box is displayed.

New tool to begin a new


query.

Page 5
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


8. Click on Design View. The Show Table box is displayed on top
with the Query window behind it.
Click on OK.

9. In the Show Table box: The field list for the Payroll Data table is
added to the upper pane of the query
Click on Payroll Data. window. The Show Table box is closed.

Click on Add.

Click on Close.

10. In the Payroll Data field list, double The query window
click on the following fields to add
them to the grid:
Last Name
First Name
Dept

Page 6
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


The query results (dynaset)
11. Click on the Run tool to run the
query and view the results.

Note that the Last Names and First


Names are two fields, displayed in
separate columns in the dynaset.

The names are not in a recognizable


order.

The Query Design view


12. Click on the View tool to return to
the Query Design view.

Page 7
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


13. You will improve the query results by
concatenating the Last Name and First The user-defined field is added to the
Name fields and sorting the list in lower pane of the query window. When creating a
alphabetical order. user-defined field
or criterion that
requires a
In the fourth column in the lower pane lengthier text
of the query window, type the display:
following: a. Place your
[Last Name]&”, ”&[First Name] cursor in the
area where
You are creating a user-defined field.
you require
a. The field names from a table in the the lengthier
database are enclosed in square brackets. display.
b. The & (ampersand) is used to connect one b. While holding
part of the user-defined field to the next down the shift
part of the user-defined field. key, press F2.
c. Anything in quotation marks represents c. Do your
text to be included in the user-defined typing.
field. d. Click on OK.
14. Click in the Sort row for the new The query grid after the changes.
user-defined field.

Click on the down arrow and choose


ascending to sort the data.
15. To delete the first two fields, Last
Name and First Name, from the grid:
a. Place the mouse pointer on the gray
rectangle at the top of each column.
It will look like a black, down
arrow. Click to select the column.
(You can select multiple columns
by clicking and holding down the
mouse button on the first column.
Then drag to select the next
columns.)
b. Press the delete key on the
keyboard.

Page 8
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


The query results
16. Click on the Run tool to run the
query.

Note the name of the user-defined


field, “Expr1.” (Expression one)
Access names the field this by default.
A second user-defined field would be
named “Expr2.” This field name can
be (and probably should be) changed.

The Query Design view


17. Click on the View tool to return
to the Query Design view.

Change the name of the user-defined


field by deleting the text Expr1 and
typing FullName. Be sure to include
the colon after the term FullName.The
colon separates the user-defined field
name from the user defined field.

The entire text in the user-defined field


reads:
FullName:[Last Name]&”, “&[First Name]
18. You can assign a new name to the
fields from the table, as well.

Click to place the cursor in front of


Dept in the field row.
19. Type Department: . Be sure to type
the colon.

The query results


20. Click on the Run tool to view the
query results.

Note the changes to the field names.

Page 9
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


21. Click on the Save tool to save the
query.
22. In the Save As box, type
EmployeeList-Q .

Click on OK.
23. Close the query results window by
clicking on its X.

Page 10
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 3: Using Calculations in a Query


When you determine the data to be stored in the tables in your database, it is not
necessary to store data that can be calculated from existing field values. For example, if
an employee is paid at an hourly rate, the annual rate does not need to be in the table. The
annual rate can be calculated in a query. Another example, in a table of customer orders,
it is not necessary to store the order total, because the total can be calculated based on the
items ordered.

In this lesson, a query is created to calculate a regular weekly paycheck based on the
regular hours and hourly rate values that are stored in the Payroll Data table.

What you do What happens


1. In the database window: The Payroll Data table is displayed in the
datasheet view.
a. Click on the object
button.
b. Double click on the Payroll Data
table icon to open the table in the
datasheet view.
c. Note the values stored in the fields
named Hours and Pay Rate.

2. Close the Payroll Data window. The New Query box is returned.
3. In the database window, click on the
object button.

4. Click on the New tool .

Page 11
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


5. With Design View highlighted, click The Query window is returned with the
on OK. Show Table box on top.

6. Double click on Payroll Data to add The Query window


the table to the query grid.

Click on Close.

7. Double click on the Last Name and The Query window with the added fields
First Name fields to add them to the
query grid.

Page 12
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


8. You will use the Expression Builder The Expression Builder box
to create the calculated field.
Alternatively, you could type the user-
defined field as you did in lesson 1.

The Expression Builder is available for


your use, whenever you want to use it.
You could have used it in lesson 1,
also.

a. Click to place the cursor in the


Field row in the third column of the
query grid.
b. Click on the Expression Builder The Expression Builder box after the
tool in the toolbar. expression is created.
c. In the Expression Builder box,
double click on the Tables folder
to open it up. The list of tables is
displayed.
d. Click once on the Payroll Data
folder to open it up. (You may
need to scroll to see the folder.) The
list of fields in the Payroll Data
table is displayed in the second
column.
e. To create the calculated expression,
double click on Hours.
Click on the * button. Double click
on Pay Rate. The expression is Should you use the Expression Builder or
typed in the edit box of the type the user-defined field yourself?
Expression Builder. a. The Expression Builder uses the full
field name, which includes the table
name. This prevents errors if there
are two or more tables in the query.
(A field name could be used in
more than one table.)
b. The Expression Builder will not
type a field name incorrectly.
c. The Expression Builder displays a
list of choices.

Page 13
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


9. Click on OK. The Query-By-Example window is
displayed.
10. Press the Home key to place the cursor The query grid with the user-defined field
at the beginning of the expression. is displayed.

Type the field name and a colon,


GrossPay:

The completed field name and user-defined


field are :
GrossPay:[PAYROLL DATA]![Hours] * [PAYROLL DATA]![Pay Rate]

The query results are returned.


11. Click on the Run tool to display
the query results.

Page 14
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


12. The GrossPay is mathematically The Field Properties box is returned.
correct, but the format is incorrect.

Click on the View tool to return to


the Query-By-Example (QBE)
window.

13. With the cursor in the user-defined


field, click on the
Properties tool in the toolbar.

14. To modify the format: The Field Properties box with the format
a. On the General page of the Field list is displayed.
Properties bar, click to place the
cursor in the Format property edit
box.
b. Click on the down arrow that
appears.
c. Click on Currency to format the
field for dollars with two decimal
places. (If you wanted to change the
number of decimal places, the
change would be made in the
Decimal Places property edit box.)
d. Click on the X to close the Field
Properties box. In Access, this also
applies the changes made. The QBE window is displayed when the
box is closed. No change is displayed in the
QBE window.

Page 15
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


The GrossPay field values are formatted for
15. Click on the Run tool to display
currency.
the query results.

The database window is displayed.


16. Click on the Save tool and name
the query GrossPay-Q .
17. Click on the X to close the Query
window.

Page 16
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Exercise 1: Working with Calculations in a Query


In this exercise, you will use the query GrossPay-Q to create another query that contains
a calculation for the first paycheck of the month. In the first paycheck of the month,
parking is deducted.

What you do What happens


1. In the database window, click on the The QBE window for GrossPay-Q is
displayed.
object button to display
the queries list.
2. Click once on GrossPay-Q to select it.
3. In the database window, click on the

tool.

4. Save the query with a new name. The Save As box is displayed in step 4.

From the menu, click on File, Save As .


5. Name the new query
FirstPayOfMonth-Q .
6. Click on OK.

The QBE window is displayed after you


click on OK.

Page 17
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


7. Modify this new query to subtract the The Expression Builder box is returned
parking rate. with the user-defined field displayed.
a. To place the cursor, click in the
Field row of the third column,
which contains the calculated field.
b. Click on the Expression
Builder tool in the toolbar.

8. Continue to create the expression: The revised expression is displayed in


a. Click to place the cursor at the end step e.
of the expression.
b. Click on the minus symbol or type
a minus.
c. Double click on the Tables folder to
open it.
d. Click once on the Payroll Data
folder to open it. (You may need to
scroll in order to see the folder.)
e. In the field list for Payroll Data,
which appears in the second column
of the box, double click on Park
Rate to add the field to the
expression.

9. Click on OK. The Expression Builder is closed and the


QBE window is displayed.

Page 18
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


10. Click on the Run tool to see the results. The query results are displayed.

Problem: Why are there no field values


for GrossPay for some of the people?
You need to resolve this.
11. Click on the Save tool in the Access
toolbar to save the query changes.

12. Close the query window by clicking on The database window is displayed.
its X.

Page 19
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


13. Review the Payroll Data table. The Payroll Data table is displayed.

a. Click on the object


button to display the Tables list.
b. Double click on the Payroll Data
table to view the data.

Do you see how to resolve the


problem? (Hint: Think of the
equation that your expression for
GrossPay is using. Remember that
the expression was calculating
correctly until you subtracted the
Park Rate.) The query datasheet is displayed after the
data is corrected.
STOP HERE AND WAIT FOR
YOUR INSTRUCTOR’S
DIRECTIONS.
c. Correct the errors.
14. Run your query again.

It should be calculating correctly.

Is there a general rule that could be


applied that would help you to
avoid this type of situation in the
future?
15. Close the company database. The Access application window remains
open.

Page 20
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 4: Creating Totals Queries


At The University of Akron, many of us frequently need to count the number of students
or employees that meet certain criteria. In Access, you can use a totals query to do this. A
totals query also can sum, find the average, minimum, maximum, standard deviation,
variance, and first or last record.

A totals query has a Total row added to the grid in the QBE window. To add the Total
row, you need to be in the design view of a query. Then, you click on the Totals tool
in the toolbar or select View, Totals from the Access main menu. The options for the
Total row are:

OPTIONS FOR TOTAL ROW2


Category Options in Drop Down Comments
List
Group By Group By Groups common records
together against which
Access performs aggregate
calculations
Aggregate Count Specifies a mathematical or
Sum selection operation to
Avg perform against a field
Max
Min
StDev
Var
First
Last
Expression Expression Use to create a calculated
field by using one or more
aggregate calculations.
Total Field Record Limit Where Use to specify limiting
criteria against an aggregate
type field. The limiting
criteria are performed
before the aggregate options
are executed.
In the following lesson and its exercise, you will create two total queries. In the first
query, you will count the number of courses offered and calculate the average cost per

2
Cary N. Prague and Michael R. Irwin with Jennifer Reardon. Microsoft Access 2000 Bible, Gold Edition.
(New York: Hungry Minds, 1999), 713-716.

Page 21
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

course and average number of students per course. In the second query, you will establish
limiting criteria for the total query.

In this first query, you will count the number of courses offered and calculate the average
cost per course and average number of students per course.

What you do What happens


The Open dialog box is displayed.
1. Click on the tool.
2. Double click on LearnUSA4. The security message is displayed.
3. Click on Open. The LearnUSA4 database is opened. The
database window is displayed.
The list of Tables is displayed.
4. Click on the objects
button.

5. Double click on the Classes table to The Classes table is opened and closed.
display it in the datasheet view. The database window is displayed.
6. Review the records to note the types of
data that are stored in the table.
7. Close the Classes table by clicking on
its X.

The New Query dialog box is displayed.


8. Click on the objects
button.

9. Click on the tool in the


database window.

10. In the New Query dialog box, click on The QBE window is opened. The Show
Design View and OK. Table dialog box is opened and displayed
on top.

Page 22
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


11. On the Tables page of the dialog box, The QBE window looks like this.
double click on Classes.
12. Click on the Close button.
13. In the field list for Classes, double click
on the following fields to select them
for the query:
ClassID
Cost
Max Students

14. To add the Total row, click on the The query is changed to a Totals query and
the aggregate functions for each field are
Totals tool in the toolbar.
selected.
(Alternatively, you can choose View,
Totals from the Access main menu.)
15. In the Total row in the grid, click on the
down arrow for the ClassID field and
select Count. (You must click on that
cell in the grid in order for the down
arrow to display.)
16. In the Total row, click on the down
arrow for the Cost field and select Avg.
17. In the Total row, click on the down
arrow for the Max Students and select
Avg.

The query results are displayed in the


18. Click on the Run tool.
datasheet view.

The format for the Avg of Max Students


can be changed in the Query Design view.

Page 23
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


19. Click on the View tool to return to The QBE window is returned and the Field
the QBE window. Properties dialog box is displayed.
20. Click to place the cursor in the Max
Students field.
21. Select the Properties tool in the
toolbar.
22. Set the Field Properties for the Format
and Decimal Places, similar to what is
shown here.
23. Close the Properties box by clicking on
its X.

24. You want to include only history The criterion is set.


classes in your query. Add a criterion to
the Totals query with these steps:
a. In the Classes field list, double click
on ClassCode to add that field to
the grid.
b. Select Where from the Total row
drop down list. (Where is the last
choice in the list.) NOTE: The "show" box is unchecked
c. In the criteria row for the when Where is used in a Totals query,
ClassCode type: like "*history*" . because there is no aggregate value to show
for the field. The field is being used only to
set criteria.

The query results are displayed in the


25. Click on the Run tool.
datasheet view.

26. Close the query results window by The database window is displayed.
clicking on its X. Do not save.

Page 24
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Exercise 2: Totals Query with Grouping - Use the Between Operator


You want to create a query that will display the total number of students born in 1976 by
State for each major. Follow the directions to create this query on your own.

What you do What happens


1. Create a new query based on the The QBE window looks like this:
Students table.
2. Select the following fields:
Major, State, StudentID, DOB
3. Click on the Totals tool in the toolbar.

4. In the Total row, set the following The QBE window looks like this:
choices:
Major: Group By
State: Group By
StudentID: Count
DOB: Where When the query is run, Access will filter
5. In the criteria row for DOB, type the the records in the Students table to use only
following criterion: the records that meet the DOB criterion.
between #01/01/76# and #12/31/76#
Then, Access will group these filtered
(The # is used by Access to indicate a records by the field values in Major, so all
date.) the students with the same major are in a
group.

Then, within each major, Access will group


again by the field values in State, so all the
students with the same major and the same
state are in a group.

The StudentIDs will be counted for each


group.

Page 25
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


The query results are displayed.
6. Click on the Run tool.

7. Close the query without saving it. The database window is displayed.

Page 26
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 5: Crosstab Query – Another Type of Totals Query


Crosstab queries present summary information in a spreadsheet format. These queries are
a special type of Totals query. The easier way to create a crosstab query is to use the
wizard. To create the query, you specify:
c Row heading: The field values of this field will be the row headings for the
dynaset. Each field value contained in this field will appear on its own row.
d Column heading: The field selected for the column heading will determine the
field values for the column headings in the dynaset.
e Value: You select the summary operation to be performed, such as count or sum,
and on which field it performs the operation.
It may be helpful to draw a sample grid of the results that you want. Then, it is easy to
answer the questions in the wizard.
In this lesson, you will create a crosstab query that shows the total number of employees
that are parking, by parking lot, and the department in which they work. Here is the
sample grid:
e d
Total Count of EE Dept MK Dept SA Dept
IDs
c Parking Lot 1
Parking Lot 2
Parking Lot 3
Parking Lot is the row heading. Dept is the column heading. We want to count the
number of IDs. Since each person has a unique ID, we are counting the number of
people.

What you do What happens


1. In the database window, click on A list of available queries is displayed.
the object button.

Page 27
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


2. In the database window, click on the The New Query box is displayed with
Crosstab Query Wizard selected.
tool.
3. Click on Crosstab Query Wizard.

4. Click on OK. The first step of the Crosstab Query Wizard


5. Click on Payroll Data to select it is displayed.
from the table list at the top of the
wizard dialog box. You may need to
scroll to see the table name.

This selects Payroll Data as the table


on which your Crosstab query is
based.

6. Click on Next. The second page of the wizard is displayed.

Page 28
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


7. Double click on Park Lot to select its Park Lot is selected as the Row Heading.
values as the Row Headings.

Note the preview area of the wizard.

8. Click on Next. Dept is selected as the column heading.


9. Click on Dept to select the
department field values for the
column headings.

Note the preview area of the wizard.

10. Click on Next. Id is selected as the field value to count.

Be certain that Id and Count are


selected.

This selects Id as the set of field


values and count as the function. (It
will count the number of Id values for
each Park Lot. Then, provide a
breakdown of the count by individual
department.)

Note the change in the preview area


of the wizard.

Page 29
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


11. Click on Next. The final screen of the wizard is displayed
with your changes.
Name the query
ParkingLotCrosstab-Q.

Accept the other defaults.

12. Click on Finish. The dynaset is returned.

What is the meaning of the data in the


first row?

What criterion could you use to


eliminate the first row from your
query results?

13. Close the query results window by The query results window is closed and the
clicking on its X. database window is displayed.

Page 30
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 6: Types of Joins


When you create a query involving two or more tables in a database, you need to tell
Access how the records in one table are related to the records in a second table. This
relationship is called a join. (If you do not do this, the results of the query will be every
record in the one table related to every record in the second table. This is called a
Cartesian product.)

There are three basic types of joins in Access:

¾ Inner joins: Access will select only those records from both tables that have a
matching value in the joined field. This is the default.
¾ Outer joins: Access will include all the records from one table and only those
records in the second table that have a matching value in the joined
field.

Left outer joins include all the records from the table on the “one”
side of the one-to-many relationship.

Right outer joins include all the records from the table on the
“many” side of the relationship.

The terms left and right come from the fact that traditionally the
“one” table is drawn to the left of the “many” table.
¾ Self joins: Access joins a table to itself in order to use the data in a single
table in a different way.

Page 31
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Part A: Inner Joins


In an inner join, the query results will contain the records from both tables, where
there is a match in the field value of the joined field. If a record exists in one table, but
has no match in the other table, the data from that record is not included in the query
results. This is the default join in Access.

What you do What happens


1. In the database window, click on the The selected table is opened in the
datasheet view. Then, the table is closed.
object button.

Open, view, and close each of these


tables to familiarize yourself with the
data:

Bonuses
Dept Codes & Names

2. In the database window, click on the The New Query dialog box is displayed.
object button.
3. Click on the tool.

4. Click on Design View to select it The Show Table box is displayed on top of
5. Click on OK. the Query window.

Page 32
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


6. Double click on The Query window is displayed with the
Dept Codes & Names to add the field lists from the two selected tables.
table to the query.
7. Double click on Bonuses to add the
table to the query.
8. In the Show Table box, click on its
X to close the box.

9. Join the field Deptcode to the field The join line appears. This creates the
Dept-Id: default join, an inner join. An inner join
a. Place the mouse pointer over shows records from the tables with a match
the field Deptcode. in the joined fields.
b. Hold down the mouse button
and drag to the field Dept-Id. The DeptCode table contains the one part
of the one-to-many relationship. The
Bonuses table contains the many part of the
relationship. One Deptcode appears in
many records in the Bonuses table.

Page 33
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


10. Select the fields for the query. The fields are added to the Query grid in
Double click on: the lower pane.

Deptname
Last Name
Bonus
11. In the Sort row of the Deptname
field, click to place the cursor.
From the down arrow, select
Ascending.

12. Click on the Run tool to view The Query results are displayed. Note that
the results. there are 15 records.
13. Scroll to view all the records. Note
that the records that are shown are
those records where the Deptcode
in the one table matched the Dept-
Id in the second table.

There are no Departments listed


without a person’s record from the
second table.

Also, there are no people listed


without a department.

These are the results of the inner


join, which is the default type of
join.

14. Click on the View tool to The QBE window is displayed.


return to the QBE window.

Page 34
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Part B: Left Outer Joins


In a left outer join, all the records from the table on the one side of the relationship
are included in the query results with any matching records from the table on the many
side of the relationship. You will see all the records from the “one” table.

What you do What happens


1. To change or view the type of join: After step 10a, the selected join line
a. Click on the join line to select appears in bold.
it.
b. From the menu, click on View,
Join Properties. In the Join
Properties box, note the current
choices for the inner join.
c. If you wish to make a change,
click on the option button for
the choice you want. In this After step 10c, the Join Properties box is
example, click on choice 2, displayed with choice 2 selected. This is the
which is the left outer join. left outer join.
d. Click on OK. The QBE
window is displayed.

The left table is always the table with the


one side of the relationship. The table’s
physical placement in the Query-By-
Example window does not matter.

The right table is the table with the many


side of the relationship. The table’s
physical placement in the Query-By-
Example window does not matter.

Page 35
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


2. Click on the Run tool to view There are 16 records in the dynaset.
the results of the left outer join.

Note the record that is in this


dynaset, but was not in the previous
dynaset.

This is a left outer join. All the


records from the one table are
shown. The additional record here
is the one for the Shipping and
Receiving department. This record
was not shown in the previous
query, because there are no
matching records in the Bonuses
table. No one in Shipping and
Receiving was paid a bonus.

Changing the join gave you that


piece of information.

3. Click on the Save tool . The Save As box


4. Name the query BonusByDept-Q .

5. Click on OK. The datasheet view of the query is


displayed.

Page 36
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Part C: Right Outer Joins


You will change the join in the query BonusByDept-Q to a right outer join. All the
records from the many table will be included in the query results with any records
from the one table that have a matching value in the joined field.

What you do What happens


The QBE window is displayed.
1. Click on the View tool .

2. Change the type of join to a right After step 2a, the selected join line is
outer join: displayed in bold.
a. Click on the join line.
b. From the menu, click on View,
Join Properties.
c. Click on the radio button for
choice 3, which is the right
outer join.
d. Click on OK.
After step2c, the Join Properties box is
displayed with choice 3 selected.

After step 2d, the QBE window is displayed.

Page 37
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


3. Click on the Run tool to see The dynaset for the BonusByDept-Q query is
the results. displayed.

Note that there are 16 records in


the dynaset.

Look at the first record, which is a


record from the many table. All the
records in the many table are
included. Note that the Department
field is null. There is no field value
for Dept-Id in the many table. One
person was paid a bonus that was
not related to a particular
department.

Changing the join gave you that


piece of information.

4. Close the dynaset’s window by The database window is displayed.


clicking on its X. Do not save the
query.

Page 38
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 7: Using the And Operator


The way to use the And operator in a query is to write the criteria on the same criteria
row. Access interprets that as all records must meet the first criterion AND the second
criterion (AND the third criterion, etc.) to be included in the query results.

An example would be all the records where Dept equals SA AND the Hired date is
greater than or equal to 10/01/02.

What you do What happens


1. In the database window, click on the The list of query objects is displayed.
object button, if
necessary.
2. In the database window, click on the The New Query dialog box is displayed
with Design View highlighted.
tool.
3. In the New Query dialog box, click on
Design View.

4. Click on OK. The Show Table dialog box is displayed


with the QBE window behind it.

Page 39
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


5. On the Tables page of the Show Table The New Department Records table is
dialog box, double click on New added to the QBE window. The Show
Department Records. Table box is closed. The QBE window is
6. In the Show Table dialog box, click on displayed.
Close.

7. In the field list, double click on the The fields are added to the query grid.
following fields to select them:

Dept
Last Name
First Name
Hired

8. In the Criteria row for Dept, enter SA. The query grid after step 9
9. In the Criteria row for Hired, enter:
>=#10/01/92#

This expression is read as “greater than


or equal to 10/01/92.” The criteria reads “where Dept is equal to
SA and Hired is greater than or equal to
If you do not like to use the 10/01/92.” When criteria are written on the
mathematical symbols, you can enter same row, Access reads and between the
the words: fields.
greater than or equal to #10/01/92#

Page 40
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


The query results are displayed in the
10. Click on the Run tool .
Datasheet view.

11. Click on the View tool . The Design view of the query is displayed.

Page 41
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 8: Using the Or Operator


Here are two ways to use the OR operator.

A. One way is to write the criteria on separate rows. Access interprets that as all records
must meet the first criterion OR the second criterion. For example, you could ask for
the records where the employee selected the A1 or A2 option for Health benefits.

You may use as many rows as you need. You could ask to see the employees who
selected A1 or A2 or C option for Health benefits.

B. On a single criteria row for a field, use the OR operator. For example, you could ask
for the records where the employee selected an option that began with the letter A or
the option C.

What you do What happens


1. Delete the criteria with these steps. The criteria row is selected in step 1b.
a. In the QBE grid, place your mouse
pointer at the beginning of the
criteria row. It will look like a
black arrow.
b. With the mouse pointer appearing The criteria in the row are deleted in step
as a black arrow, click to select the 1c.
criteria row.
c. Press the Delete key on the
keyboard.

Page 42
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

What you do What happens


2. Double click on the Health field in the The Health field is added to the grid.
field list.
3. In the first criteria row for the Health The criteria reads “show me the records
field, type A1. (The digit one is after where the Health field value is equal to A1
the A.) or A2.”
4. In the second criteria row for the
Health field, type A2.

5. Click on the Run tool . The query results are displayed.

6. Delete A2 from the second criteria The criterion is modified.


row.
7. In the first criteria row for Health,
modify the criterion to read:
“A1” or “A2”.

8. Click on the Run tool . The query results are displayed. The same
dynaset is returned, because the criterion is
the same.

9. Click on the View tool . The QBE window is displayed.

Page 43
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Lesson 9: Using the And and Or Operators Together


In this lesson, you first want to view the records for employees in the SA department who
have a Health choice of A1 or B. Then, the criteria are modified.

You need to use both the AND and OR operators. When you write the query, remember
that Access uses the AND operator for criteria that are written on the same row.

What you do What happens


1. On the criteria row for the Dept field, The criteria ask for the records where the
enter SA. Dept is equal to SA AND Health is equal to
2. Modify the criteria row for the Health A1 OR B.
field to read “A1” or “B”.

3. Click on the Run tool . The query results are displayed.

The QBE window is displayed.


4. Click on the View tool .
5. Modify the criteria: The criteria asks for the records where:
a. Delete the words Or “B” from the
criteria row for Health. Dept is equal to SA AND Health is equal to
b. Enter B on the second criteria row A1
for Health. OR
Health is equal to B.

6. Click on the Run tool . The query results are displayed.


7. Review the results and compare them
to the results in step 3.

Page 44
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Appendix A: Selecting the Highest Values


To see the records for the highest values in a query, use the Top Values tool. This tool is
in the toolbar that is available when in the Design view of a query.

First, create the query that lists the results from which you want to select the highest
values. Then, select the total number of highest values or the percentage of highest values
that you want to see. This selection can be made at the down arrow of the Top Values
tool or by typing the number or percentage in the tool’s edit box.

For example, to find the people with the three highest pay rates:
1. Create a query that lists the fields and records you want to use. Here you see the Last
Name, First Name and Pay Rate fields from the Payroll Data table.

Top Values
tool

2. Click to place the cursor in the field to be used for the Top Values criterion. In this
example, it is the Pay Rate field.
3. Click in the edit box for the Top Values tool. (It says ALL by default, meaning all
the values that meet the query’s criteria.) Enter the number 3 and press the ENTER
key.

Alternatively, you can click on the down arrow of the Top Values tool and select a
number of top values to return or a percentage of top values to return. Also, you can
type a percentage in the edit box of the Top Values tool. For example, you can type
10%.

Page 45
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

4. Click on the Run tool to run the query. The records returned will be those that
meet your selected value in the Top Values tool.

Here are the records from the Payroll Data table with the 3 highest values in the Pay
Rate field.

It is possible to have more than 3 records returned when you request the 3 Top Values.
This could happen if two or more people had the same pay rate. For example, if
Vicki Smith’s pay rate was $21.50, her record would be displayed. You asked for the top
3 pay rates, which are $24.00, $21.50, and $15.50. All employees with those rates would
be included in the query results.

Are you wondering how you would select the lowest value? Use a Totals query and the
function Min (for lowest value).

Page 46
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron

Appendix B: Updating/Editing Data in a Query Dynaset


In general, you can change data in a query dynaset and your changes are saved to the
tables. However, here are some restrictions on editing data in a dynaset:3
1. To update a table from a query, a value in a specific row in the query must
represent a single record in the underlying table.
2. A primary key field cannot be edited if referential integrity is in effect and if the
field is part of a relationship (unless Cascade Updates is set to Yes).
3. You cannot update fields in a Crosstab or Totals query because these queries
group records together.
4. Calculated fields cannot be edited.

If you do not want any fields to be edited in a query dynaset, set the Recordset Type
property of the query to Snapshot. (The default setting for this property is Dynaset.)
When this property is set to Snapshot, a person can view the query results but cannot edit
the records.

To set this property, click in an empty area of the top half of the query window. This part
of the window is where the field lists are displayed. Then, click on the Properties
tool in the toolbar. Be sure that the dialog box that is displayed is for Query
Properties. Click on the down arrow for Recordset Type to make a selection.

3
Cary N. Prague, Michael R. Irwin and Jennifer Reardon. Microsoft Access 2003 Bible, Gold Edition.
(New York:Wiley Publishing, Inc., 2004), 188-191.

Page 47
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.

Das könnte Ihnen auch gefallen