Tải bản đầy đủ (.pdf) (30 trang)

Tài liệu Microsoft Access 2007 Data Analysis P1 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (578.29 KB, 30 trang )


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page iii

Microsoft® Access™ 2007
Data Analysis
Michael Alexander


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page ii


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page i


Microsoft® Access™ 2007
Data Analysis


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page ii


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page iii

Microsoft® Access™ 2007
Data Analysis
Michael Alexander


01_104859 ffirs.qxp

2/20/07


3:02 PM

Page iv

Microsoft® Access™ 2007 Data Analysis
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-10485-9
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted
in any form or by any means, electronic, mechanical, photocopying, recording, scanning or
otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222
Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the
Publisher for permission should be addressed to the Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or
online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this
work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every
situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is
required, the services of a competent professional person should be sought. Neither the
publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information
the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services or to obtain technical support,
please contact our Customer Care Department within the U.S. at (800) 762-2974, outside the

U.S. at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Cataloging-in-Publication Data Available from Publisher
Trademarks: Wiley, the Wiley logo, and related trade dress are trademarks or registered
trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other
countries, and may not be used without written permission. Microsoft and Access are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other
countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears
in print may not be available in electronic books.


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page v

For Mary, Ethan, and Emma


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page vi



01_104859 ffirs.qxp

2/20/07

3:02 PM

Page vii

About the Author

Michael Alexander is a Microsoft Certified Application Developer
(MCAD) with more than 14 years experience consulting and developing
office solutions. He currently lives in Plano, TX where he serves as a Senior
Program Manager for a top technology firm. In his spare time he runs a free
tutorial site, www.datapigtechnologies.com, where he shares basic
Access and Excel tips to the Office community.

vii


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page viii


01_104859 ffirs.qxp


2/20/07

3:02 PM

Page ix

Credits

Acquisitions Editor
Katie Mohr

Project Coordinator
Patrick Redmond

Development Editor
Kelly Talbot

Graphics and Production
Specialists
Jennifer Mayberry, Barbara Moore,
Rashelle Smith

Technical Editor
Todd Meister
Production Editor
Angela Smith
Copy Editor
Travis Henderson
Editorial Manager

Mary Beth Wakefield
Production Manager
Tim Tate

Quality Control Technician
John Greenough
Proofreading
Lisa Stiers
Indexing
Aptara
Anniversary Logo Design
Richard J. Pacifico

Vice President and Executive
Group Publisher
Richard Swadley
Vice President and Executive
Publisher
Joseph B. Wikert

ix


01_104859 ffirs.qxp

2/20/07

3:02 PM

Page x



02_104859 ftoc.qxp

2/17/07

12:49 AM

Page xi

Contents

About the Author

ix

Acknowledgments

xix

Introduction

xxi

Part I

Fundamentals of Data Analysis in Access

1


Chapter 1

The Case for Data Analysis in Access
Where Data Analysis with Excel Can Go Wrong

3
3

Scalability
Transparency of Analytical Processes
Separation of Data and Presentation

4
6
7

Deciding Whether to Use Access or Excel

8

Size of Data
Data Structure
Data Evolution
Functional Complexity
Shared Processing

An Excel User’s Guide to Access: Don’t Panic!
Tables
Queries
Reports

Macros and VBA

Chapter 2

9
9
9
10
10

10
11
11
12
12

Summary

13

Access Basics
Access Tables

15
15

Table Basics
Opening a Table in the Datasheet View
Identifying Important Table Elements


16
16
17

xi


02_104859 ftoc.qxp

xii

2/17/07

12:49 AM

Page xii

Contents
Opening a Table in the Design View
Exploring Data Types
Before Creating a Table
Creating a Table with Design View
Field Properties
Primary Key
Getting Data into Access
Importing
Linking
Things to Remember About Importing Data
Importing Data from an Excel Spreadsheet
Importing Data from a Text File


Understanding the Relational Database Concept
Why Is This Concept Important?
Excel and the Flat-File Format
Splitting Data into Separate Tables
Foreign Keys
Relationship Types
Referential Integrity
Cascading Updates and Deletes

Query Basics
What Is a Query?
Creating Your First Select Query
Sorting Query Results
Filtering Query Results
Querying Multiple Tables
Refining the Query
Using Operators in Queries
Exporting Query Results

Chapter 3

17
19
20
21
24
26
28
28

28
28
29
30

30
30
31
33
34
34
36
37

37
37
37
40
42
43
45
46
49

Summary

49

Beyond Select Queries
Aggregate Queries


51
51

Creating an Aggregate Query
About Aggregate Functions
Group By
Sum, Avg, Count, StDev, Var
Min, Max, First, Last
Expression, Where

Action Queries
Why Use Action Queries?
Make-Table Queries
Why Use a Make-Table Query?
What Are the Hazards of Make-Table Queries?
Creating a Make-Table Query
Delete Queries
Why Use a Delete Query?
What Are the Hazards of Delete Queries?
Creating a Delete Query

52
56
56
57
58
58

61

62
62
62
62
63
65
65
65
66


02_104859 ftoc.qxp

2/17/07

12:49 AM

Page xiii

Contents
Append Queries
Why Use an Append Query?
What Are the Hazards of Append Queries?
Creating an Append Query
Update Queries
Why Use an Update Query?
What Are the Hazards of Update Queries?
Creating an Update Query
A Word on Updatable Datasets


Crosstab Queries
Using the Crosstab Query Wizard
Creating a Crosstab Query Manually
Using the Query Design Grid to Create Your Crosstab
Query
Customizing Your Crosstab Queries

68
68
69
71
74
75
75
75
78

78
79
84
85
88

Summary

90

Part II

Basic Analysis Techniques


93

Chapter 4

Transforming Your Data with Access
Finding and Removing Duplicate Records

95
96

Defining Duplicate Records
Finding Duplicate Records
Removing Duplicate Records

Common Transformation Tasks
Filling in Blank Fields
Concatenating
Concatenating Fields
Augmenting Field Values with Your Own Text
Changing Case
Removing Leading and Trailing Spaces from a String
Finding and Replacing Specific Text
Adding Your Own Text in Key Positions Within a String
Parsing Strings Using Character Markers
Query 1
Query 2

Chapter 5


96
97
100

102
102
104
104
105
107
109
110
112
116
118
119

Summary

120

Working with Calculations and Dates
Using Calculations in Your Analysis

121
121

Common Calculation Scenarios
Using Constants in Calculations
Using Fields in Calculations

Using the Results of Aggregation in Calculations
Using the Results of One Calculation as an Expression
in Another
Using a Calculation as an Argument in a Function
Using the Expression Builder to Construct Calculations

122
122
123
124
124
125
126

xiii


02_104859 ftoc.qxp

xiv

2/17/07

12:49 AM

Page xiv

Contents
Common Calculation Errors
Understanding the Order of Operator Precedence

Watching Out for Null Values
Watching the Syntax in Your Expressions

Using Dates in Your Analysis
Simple Date Calculations
Advanced Analysis Using Functions
The Date Function
The Year, Month, Day, and Weekday Functions
The DateAdd function
Grouping Dates into Quarters
The DateSerial Function

Chapter 6

130
130
131
133

134
134
135
135
139
141
143
145

Summary


146

Performing Conditional Analysis
Using Parameter Queries

149
149

How Parameter Queries Work
Ground Rules of Parameter Queries
Working with Parameter Queries
Working with Multiple Parameter Conditions
Combining Parameters with Operators
Combining Parameters with Wildcards
Using Parameters as Calculation Variables
Using Parameters as Function Arguments

Using Conditional Functions
The IIf Function
Using IIf to Avoid Mathematical Errors
Using IIf to Save Time
Nesting IIf Functions for Multiple Conditions
Using IIf Functions to Create Crosstab Analyses
The Switch Function
Comparing the IIf and Switch Functions

Summary

151
151

152
152
153
154
155
156

159
159
159
161
163
164
166
167

169

Part III

Advanced Analysis Techniques

171

Chapter 7

Understanding and Using SQL
Understanding Basic SQL

173

173

The SELECT Statement
Selecting Specific Columns
Selecting All Columns
The WHERE Clause
Making Sense of Joins
Inner Joins
Outer Joins

175
175
176
176
177
177
178

Getting Fancy with Advanced SQL Statements
Expanding Your Search with the Like Operator
Selecting Unique Values and Rows without Grouping

179
180
181


02_104859 ftoc.qxp

2/17/07


12:49 AM

Page xv

Contents
Grouping and Aggregating with the GROUP BY Clause
The HAVING Clause
Setting Sort Order with the ORDER BY Clause
Creating Aliases with the AS Clause
Creating a Column Alias
Creating a Table Alias
SELECT TOP and SELECT TOP PERCENT
Top Values Queries Explained
SELECT TOP
SELECT TOP PERCENT
Performing Action Queries via SQL Statements
Make-Table Queries Translated
Append Queries Translated
Update Queries Translated
Delete Queries Translated
Creating Crosstabs with the TRANSFORM Statement

Using SQL Specific Queries
Merging Datasets with the UNION Operator
Creating a Table with the CREATE TABLE Statement
Manipulating Columns with the ALTER TABLE Statement
Adding a Column with the ADD Clause
Altering a Column with the ALTER COLUMN Clause
Deleting a Column with the DROP COLUMN Clause


Chapter 8

182
183
183
183
184
184
184
184
186
187
187
187
188
188
188
188

189
189
191
192
192
193
193

Summary


193

Subqueries and Domain Aggregate Functions
Enhancing Your Analysis with Subqueries

195
196

Why Use Subqueries?
Subquery Ground Rules
Creating Subqueries without Typing SQL Statements
Using IN and NOT IN with Subqueries
Using Subqueries with Comparison Operators
Using Subqueries as Expressions
Using Correlated Subqueries
Uncorrelated Subqueries
Correlated Subqueries
Using a Correlated Subquery as an Expression
Using Subqueries within Action Queries
A Subquery in a Make-Table Query
A Subquery in an Append Query
A Subquery in an Update Query
A Subquery in a Delete Query

Domain Aggregate Functions
Understanding the Different Domain Aggregate Functions
DSum
DAvg
DCount
DLookup


197
197
198
201
201
202
203
203
203
205
205
205
205
206
206

208
210
210
210
211
211

xv


02_104859 ftoc.qxp

xvi


2/17/07

12:49 AM

Page xvi

Contents
DMin and DMax
DFirst and DLast
DStDev, DStDevP, DVar, and DvarP
Examining the Syntax of Domain Aggregate Functions
Using No Criteria
Using Text Criteria
Using Number Criteria
Using Date Criteria
Using Domain Aggregate Functions
Calculating the Percent of Total
Creating a Running Count
Using a Value from the Previous Record

Chapter 9

211
211
211
212
212
212
213

213
214
214
215
217

Summary

219

Running Descriptive Statistics in Access
Basic Descriptive Statistics

221
222

Running Descriptive Statistics with Aggregate Queries
Determining Rank, Mode, and Median
Ranking the Records in Your Dataset
Getting the Mode of a Dataset
Getting the Median of a Dataset
Pulling a Random Sampling from Your Dataset

Advanced Descriptive Statistics
Calculating Percentile Ranking
Determining the Quartile Standing of a Record
Creating a Frequency Distribution

Summary
Chapter 10 Analyzing Data with Pivot Tables and Pivot Charts

Pivot Tables in Access?
The Anatomy of a Pivot Table
The Totals and Detail Area
The Row Area
The Column Area
The Filter Area
Creating a Basic Pivot Table
Creating an Advanced Pivot Table with Details
Saving Your Pivot Table
Sending Your Access Pivot Table to Excel
Pivot Table Options
Expanding and Collapsing Fields
Changing Field Captions
Sorting Data
Grouping Data
Using Date Groupings
Filtering for Top and Bottom Records
Adding a Calculated Total

222
223
224
225
227
229

231
231
233
235


240
241
242
243
243
244
245
245
246
250
252
253
254
255
255
256
256
259
260
261


02_104859 ftoc.qxp

2/17/07

12:49 AM

Page xvii


Contents
Working with Pivot Charts in Access
The Data Area
The Series Area
The Category Area
The Filter Area
Creating a Basic Pivot Chart
Formatting Your Pivot Chart

Summary

Part IV

Automating Data Analysis

Chapter 11 Scheduling and Running Batch Analysis
Introduction to Access Macros
Dealing with Access 2007 Security Features
The Quick Fix
The Long-Term Fix
Creating Your First Macro
Essential Macro Actions
Manipulating Forms, Queries, Reports, and Tables
The Access Environment
Executing Processes
Outputting Data

Setting Up and Managing Batch Analysis
Getting Organized

Using a Logical Naming Convention
Using the Description Property
Setting Up a Basic Batch Analysis
Building Smarter Macros
Simulating If...Then
Simulating If...Then...Else
Looping with Macros

Scheduling Macros to Run Nightly
Using an AutoExec Macro to Schedule Tasks
Using the Windows Task Scheduler
Using Command Lines to Schedule Tasks
When to Use Command Lines to Schedule Tasks
Instead of AutoExec
Scheduling a Macro to Run Using a Command Line

Summary
Chapter 12 Leveraging VBA to Enhance Data Analysis
Creating and Using Custom Functions
Creating Your First Custom Function
Creating a Custom Function that Accepts Arguments

Controlling Analytical Processes with Forms
The Basics of Passing Data from a Form to a Query
Enhancing Automation with Forms
Enumerating Through a Combo Box

265
265
265

266
267
268
269

272

353
275
276
277
278
279
280
282
283
283
284
284

285
285
285
288
289
291
292
294
296


301
301
302
307
308
308

310
311
312
313
315

319
320
324
326

xvii



×