39
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert Grauer, Keith Mulbery, Maurie Wigman Lockley Exploring Microsoft Office Access 2007

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Embed Size (px)

Citation preview

Page 1: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11

Committed to Shaping the Next Generation of IT Experts.

Chapter 7

Advanced QueriesRobert Grauer, Keith Mulbery, Maurie Wigman Lockley

Exploring Microsoft Office Access 2007

Page 2: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2

Objectives

Maintain a database Understand action queries Create an Update query Use a make table query Create a delete query

Page 3: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 3

Objectives

Construct a crosstab query Work with parameter queries Create find unmatched queries Understand find duplicate queries

Page 4: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 4

Action Queries

Change underlying table data Select queried do not

Can be used to create new tables Cannot be undone Play a large role in maintaining a database

Page 5: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 5

About Update Queries

Changes value(s) in one/ more records

Use the Update query button Database Tools tab,

Query Type group

Update button

Page 6: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 6

Creating an Update query

Identify record source Show desired tables in

design grid Switch from Select to

Update Query Insert Field to be

updated Enter the value to be

updated Value entered in Update

To field

Update value

Page 7: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 7

Testing Action Queries

Test all action queries before execution Choose Datasheet View

Allows preview of results before execution

Check Datasheet View first

Page 8: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 8

Running an Update Query

Click the Run button Design tab, Results group

View table to see the results

Run button

Before Update query

After Update query

Page 9: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 9

Specifying Criteria in an Action Query

Specified in the field being update Also specified using an additional field(s)

Criteria

Page 10: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 10

Using Criteria with an Update Query

Specifies specific records for update Works the same as in a select query

After update query with criteria

Before update query with criteria

Criteria

Page 11: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11

About Append Queries

Adds records from one table to another Data types between tables must match Validation rules in existing table apply

Records not added if validation rule violated Source table fields may be ignored

Do not add to design grid If auto-number fields exists in target table

Do not import auto-number fields

Page 12: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 12

Creating an Append Query

Identify target and source tables Click Append button Choose Target table from Append box

May also browse to choose another database

Append dialog box

Append button

Page 13: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 13

Creating an Append Query

Add fields from Source table to design grid Test in Datasheet view Run query

Fields to append added to design grid

Page 14: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 14

Append Query Results

Data is appended to existing table

February Agents before append

September Agents before and after append

February Agents after append

Page 15: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 15

About a Make Table Query

Almost the same as append query Table must preexist in append

query No pre-existing table in make

table query Target database must exist

Page 16: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 16

Creating a Make Table Query

Identify the source table Click Make Table button Enter table name or choose database

Accomplished in the Make table dialog box

Make Table dialog box

Make Table button

Page 17: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 17

Running a Make Table Query

Add fields to design grid Test in Datasheet view Run query

Fields added in design grid

Test in Datasheet view

Page 18: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 18

Make Table Query Results

New table added to Objects pane Open table to view results

Source Table

Results of make table query

Page 19: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 19

About Delete Queries

Can delete all records from table Criteria used to delete specific records If no criteria, all records deleted

Very powerful – cannot be undone Setup is same as other action queries

Page 20: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 20

Creating a Delete Query

Add table to Design grid Click Delete Button Add field(s) to design grid

Use criteria to specify delete conditions

Add table to design grid

Delete button

Delete Query with criteria

Page 21: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2121

Running a Delete Query

Test query in Datasheet view Run Query Open table to view results

Test in Design view

Table before delete query

Table after delete query

Page 22: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 22

Hiding a Query

Prevents accidental execution of action queries Right-click query Click the Hidden checkbox

All queries shown

Some queries hidden

Page 23: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2323

About Crosstab Queries

Summarizes by two sets of unrelated facts Output resembles a PivotTable

PivotTable primarily for management decision making

Crosstab query primarily for end users Will not work on related data Works with one or more tables

Page 24: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2424

Crosstab Query – Using the Query Wizard Step 1

Click the Query Wizard button In the New Query dialog box

Select Crosstab Query Wizard

Select Crosstab Query Wizard

Page 25: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2525

Using the Query Wizard Step 2

Select table/query to use in crosstab To use fields from multiple tables

Create query from tables first

Select source table or query to use

Page 26: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2626

Using the Query Wizard Step 3

Select fields(s) for column headings

Select field(s) for column headings

Page 27: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 2727

Using the Query Wizard Step 4

Select the field to calculate Select the appropriate aggregate function

Select aggregate function

Page 28: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 28

Using the Query Wizard Step 5

Enter new query name or accept default View or modify the query

Enter new name or accept default name

Page 29: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 29

Completed Crosstab Query

Summarizes list price Per selling/listing agent per city

Source table

Crosstab query

Page 30: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 30

About Parameter Queries

Information intentionally omitted by designer Allows end user to supply criteria Customizable data request

More flexible than other query types Considered a select query Designers able to lock query design

Page 31: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 31

Creating a Parameter Query

Identify tables/ fields in design view Create prompt for user in criteria field

Prompt must be enclosed in brackets [ ] Multiple fields can be used as prompts

Message for user entered in brackets

Page 32: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 32

Running a Parameter Query

Click the Run button Enter criteria in message box

Criteria rules and use is same as normal select query View results in Datasheet view

Message prompt with criteria entered

Parameter query results

Page 33: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 33

Parameter Report

Normally based of a parameter query If not based on parameter query

Open reports source query in design view Add parameter

OR View report in Design Open property sheet for field Click Record Source property - add parameter

Page 34: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 34

About Unmatched Queries

Compares records in two related tables Returns records found in only one table Query Wizard easiest method of creation

Unmatched Query results

Page 35: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 35

Unmatched Query Wizard

Run Query Wizard Select Find Unmatched Query Wizard

Find Unmatched Query

Page 36: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 36

Unmatched Query Wizard

Select first table source

Select second table source

Select first table source

Select second table source

Page 37: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 37

Unmatched Query Wizard

Select the duplicate fields Click the <=> button

Select display fields for query results

Click Finish

Select duplicate fields

Select fields for query results

Page 38: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 38

About Find Duplicates Query

Finds duplicate data Aids in data entry error detection Allows administrative use of purposely duplicated data Easily created with the Query Wizard

Query Wizard

Find Duplicates query result

Page 39: Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Committed to Shaping the Next Generation of IT Experts. Chapter 7 Advanced Queries Robert

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 39Copyright © 2008 Pearson Prentice Hall. All rights reserved. 39