Append Queries 68Using the Query Design Grid to Create Your Crosstab Finding and Removing Duplicate Records 96 Augmenting Field Values with Your Own Text 105 Removing Leading and Traili
Trang 2Michael Alexander
Data Analysis
Trang 4Microsoft ® Access ™ 2007
Data Analysis
Trang 6Michael Alexander
Data Analysis
Trang 7Microsoft ® 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 Copy- right 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 http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty:The publisher and the author make no sentations 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 fit- ness for a particular purpose No warranty may be created or extended by sales or promo- tional 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 ren- dering 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 orga- nization or Website is referred to in this work as a citation and/or a potential source of fur- ther 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, read- ers should be aware that Internet Websites listed in this work may have changed or disap- peared between when this work was written and when it is read.
repre-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 trade- marks 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 Publish- ing, 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.
Trang 8For Mary, Ethan, and Emma
Trang 10Michael Alexander is a Microsoft Certified Application Developer(MCAD) with more than 14 years experience consulting and developingoffice solutions He currently lives in Plano, TX where he serves as a SeniorProgram Manager for a top technology firm In his spare time he runs a freetutorial site, www.datapigtechnologies.com, where he shares basicAccess and Excel tips to the Office community.
About the Author
vii
Trang 14About the Author ix
Deciding Whether to Use Access or Excel 8
Contents
xi
Trang 15Opening a Table in the Design View 17
Understanding the Relational Database Concept 30
Chapter 3 Beyond Select Queries 51
What Are the Hazards of Make-Table Queries? 62
Trang 16Append Queries 68
Using the Query Design Grid to Create Your Crosstab
Finding and Removing Duplicate Records 96
Augmenting Field Values with Your Own Text 105
Removing Leading and Trailing Spaces from a String 109
Adding Your Own Text in Key Positions Within a String 112
Chapter 5 Working with Calculations and Dates 121
Using Calculations in Your Analysis 121
Using the Results of Aggregation in Calculations 124 Using the Results of One Calculation as an Expression
Using a Calculation as an Argument in a Function 125 Using the Expression Builder to Construct Calculations 126
Contents xiii
Trang 17Common Calculation Errors 130 Understanding the Order of Operator Precedence 130
The Year, Month, Day, and Weekday Functions 139
Chapter 6 Performing Conditional Analysis 149
Working with Multiple Parameter Conditions 152
Using Parameters as Calculation Variables 155
Nesting IIf Functions for Multiple Conditions 163 Using IIf Functions to Create Crosstab Analyses 164
Part III Advanced Analysis Techniques 171 Chapter 7 Understanding and Using SQL 173
Getting Fancy with Advanced SQL Statements 179
Expanding Your Search with the Like Operator 180 Selecting Unique Values and Rows without Grouping 181
Trang 18Grouping and Aggregating with the GROUP BY Clause 182
Setting Sort Order with the ORDER BY Clause 183
Performing Action Queries via SQL Statements 187
Creating Crosstabs with the TRANSFORM Statement 188
Creating a Table with the CREATE TABLE Statement 191 Manipulating Columns with the ALTER TABLE Statement 192
Altering a Column with the ALTER COLUMN Clause 193 Deleting a Column with the DROP COLUMN Clause 193
Chapter 8 Subqueries and Domain Aggregate Functions 195
Enhancing Your Analysis with Subqueries 196
Creating Subqueries without Typing SQL Statements 198
Using a Correlated Subquery as an Expression 205
Understanding the Different Domain Aggregate Functions 210
Trang 19DMin and DMax 211
Examining the Syntax of Domain Aggregate Functions 212
Chapter 9 Running Descriptive Statistics in Access 221
Running Descriptive Statistics with Aggregate Queries 222
Pulling a Random Sampling from Your Dataset 229
Determining the Quartile Standing of a Record 233
Chapter 10 Analyzing Data with Pivot Tables and Pivot Charts 241
Creating an Advanced Pivot Table with Details 250
Trang 20Working with Pivot Charts in Access 265
Part IV Automating Data Analysis 353 Chapter 11 Scheduling and Running Batch Analysis 275
Manipulating Forms, Queries, Reports, and Tables 283
Setting Up and Managing Batch Analysis 285
Scheduling Macros to Run Nightly 301
When to Use Command Lines to Schedule Tasks
Scheduling a Macro to Run Using a Command Line 308
Chapter 12 Leveraging VBA to Enhance Data Analysis 311
Creating and Using Custom Functions 312
Creating a Custom Function that Accepts Arguments 315
Controlling Analytical Processes with Forms 319
The Basics of Passing Data from a Form to a Query 320
Contents xvii
Trang 21Processing Data Behind the Scenes 329
Advanced Techniques Using RunSQL Statements 332
Passing User-Defined Parameters from a Form to Your
Chapter 13 Query Performance, Database Corruption, and Other
Final Thoughts 339
Steps You Can Take to Optimize Query Performance 340
Compacting and Repairing Your Database Regularly 343
Watching for Corruption in Seemingly Normal Databases 344 Common Errors Associated with Database Corruption 345
Steps You Can Take to Prevent Database Corruption 348 Backing Up Your Database on a Regular Basis 348 Compacting and Repairing Your Database on a Regular
Avoiding Interruption of Service while Writing to Your
Never Working with a Database from Removable Media 350
Diversifying Your Knowledgebase with Online Resources 351
Part V Appendixes 353 Appendix A Data Analyst’s Function Reference 355 Appendix B Access VBA Fundamentals 397 Appendix C Error Message Reference 411
Trang 22A big thank you to Katie Mohr for taking a chance on this project and beingsuch a wonderful project manager Many thanks to Kelly Talbot, Todd Meister,and the brilliant team of professionals who helped bring this book tofruition A special thank you to Mary who puts up with all my crazy projects.
Acknowledgments
xix
Trang 24If you were to ask a random sampling of people what data analysis is, mostwould say that it is the process of calculating and summarizing data to get
an answer to a question In one sense, they are correct However, theactions they are describing represent only a small part of the processknown as data analysis
For example, if you were asked to analyze how much revenue in salesyour company made last month, what would you have to do in order tocomplete that analysis? You would just calculate and summarize the salesfor the month, right? Well, where would you get the sales data? Wherewould you store the data? Would you have to clean up the data when yougot it? How would you present your analysis: by week, by day, by loca-tion? The point being made here is that the process of data analysis is made
up of more than just calculating and summarizing data
A more representative definition of data analysis is the process of tematically collecting, transforming, and analyzing data in order to presentmeaningful conclusions To better understand this concept, think of dataanalysis as a process that encapsulates four fundamental actions: collec-tion, transformation, analysis, and presentation
sys-■■ Collection: Collection encompasses the gathering and storing ofdata—that is, where you obtain your data, how you will receiveyour data, how you will store your data, and how you will accessyour data when it comes time to perform some analysis
■■ Transformation: Transformation is the process of ensuring your data is uniform in structure, free from redundancy, and stable This
Introduction
xxi
Trang 25generally entails things like establishing a table structure, cleaningtext, removing blanks, and standardizing data fields.
■■ Analysis: Analysis is the investigation of the component parts ofyour data and their relationships to your data source as a whole Youare analyzing your data when you are calculating, summarizing, cat-egorizing, comparing, contrasting, examining, or testing your data
■■ Presentation: In the context of data analysis, presentation deals withhow you make the content of your analysis available to a certainaudience That is, how you choose to display your results Someconsiderations that go along with presentation of your analysisinclude the platform you will use, the levels of visibility you willprovide, and the freedom you will give your audience to changetheir view
As you think about these four fundamental actions, think about this reality: Most analysts are severely limited to one tool—Excel This meansthat all of the complex actions involved in each of these fundamentals are mostly being done with and in Excel What’s the problem with that?Well Excel is not designed to do many of these actions However, manyanalysts are so limited in their toolsets that they often end up in hand-to-hand combat with their data, creating complex workarounds and ineffi-cient processes
What this book will highlight is that there are powerful functionalities inAccess that can help you go beyond your two-dimensional spreadsheetand liberate you from the daily grind of managing and maintaining redun-dant analytical processes Indeed, using Access for your data analysisneeds can help you streamline your analytical processes, increase yourproductivity, and analyze the larger datasets that have reached Excel’s limitations
Throughout this book, you will come to realize that Access is not a drydatabase program used only for storing data and building departmentalapplications Access possesses strong data analysis functionalities that areeasy to learn and certainly applicable to many types of organizations anddata systems
What to Expect from This Book
Within the first three chapters, you will be able to demonstrate proficiency
in Access, executing powerful analysis on large datasets that have longsince reached Excel’s limitations Within the first nine chapters, you will beable to add depth and dimension to your analysis with advanced Accessfunctions, building complex analytical processes with ease By the end of
Trang 26the book, you will be able to create your own custom functions, performbatch analysis, and develop automated procedures that essentially run ontheir own.
After completing this book, you will be able to analyze large amounts ofdata in a meaningful way, quickly slice data into various views on the fly,automate redundant analysis, save time, and increase productivity
What to Not Expect from This Book
It’s important to note that there are aspects of Access and data analysis thatare outside the scope of this book While this book does cover the funda-mentals of Access, it is always in the light of data analysis and it is writtenfrom a data analyst’s point of view This is not meant to be an all-encom-passing book on Access That being said, if you are a first-time user ofAccess, you can feel confident that this book will provide you with a solidintroduction to Access that will leave you with valuable skills you can use
in your daily operations
This book is not meant to be a book on data management theory and bestpractices Nor is it meant to expound on high-level business intelligenceconcepts This is more of a “technician’s” book, providing hands-oninstruction that introduces Access as an analytical tool that can providepowerful solutions to common analytical scenarios and issues
Finally, while this book does contain a chapter that demonstrates varioustechniques to perform a whole range of statistical analysis, it is important
to note that this book does not cover statistics theory, methodology, or bestpractices
Skills Required for This Book
In order to get the most out of this book, it’s best that you have certain skillsbefore diving into the topics highlighted in this book The ideal candidatefor this book will have:
■■ Some experience working with data and familiarity with the basicconcepts of data analysis such as working with tables, aggregatingdata, and performing calculations
■■ Experience using Excel with a strong grasp of concepts such as tablestructures, filtering, sorting and using formulas
■■ Some basic knowledge of Access; enough to know it exists and tohave opened a database once or twice
Introduction xxiii
Trang 27How This Book Is Organized
Part I: Fundamentals of Data Analysis in Access
Part I, which includes Chapters 1, 2, and 3, provides a condensed duction to Access Here, you will learn some of the basic fundamentals ofAccess, along with the essential query skills required throughout the rest ofthe book Topics covered in this Part are: relational database concepts,query basics, using aggregate queries, action queries, and Crosstabqueries
intro-Part II: Basic Analysis Techniques
Part II will introduce you to some of the basic analytical tools and niques available to you in Access Chapter 4 covers data transformation,providing examples of how to clean and shape raw data to fit your needs.Chapter 5 provides in-depth instruction on how to create and utilize customcalculations in your analysis Chapter 5 also shows you how to work withdates, using them in simple date calculations, or performing advanced timeanalysis Chapter 6 introduces you to some conditional analysis techniquesthat allow you to add logic to your analytical processes
tech-Part III: Advanced Analysis Techniques
Part III will demonstrate many of the advanced techniques that truly bringyour data analysis to the next level Chapter 7 covers the fundamentalsSQL statements Chapter 8 picks up from there and introduces you to sub-queries and domain aggregate functions Chapter 9 demonstrates many ofthe advanced statistical analysis you can perform using subqueries anddomain aggregate functions Chapter 10 provides you with an in-depthlook at using PivotTables and PivotCharts in Access
Part IV: Automating Data Analysis
Part IV takes you beyond manual analysis with queries and introduces you
to the world of automation Chapter 11 gives you an in-depth view of howmacros can help increase you productivity by running batch analysis.Chapter 12 demonstrates how a little coding with Visual Basic for Applica-tions (VBA) can help enhance data analysis Chapter 13 offers some finalthoughts and tips on query performance, database corruption, and how toget help in Access