Your SlideShare is downloading. ×
Accelerated Computer Training for Working Professionals Admissions Case
Orange Coast Database Associates Course (800)355-9...
Admissions AIN1021
AIN102 – Access Query Design
Introduction to Access Queries for
experienced Windows users
P.O. Box 6142...
AIN102 Notes
 This course is based on an earlier course,
SQL200A, which was focused on using SQL in
the Access environmen...
AdmissionsAIN1025
Orange Coast Database Associates
 Loose association of local University instructors and IT
Professional...
AdmissionsAIN1027
 Quick pace for experienced windows users
 Assumes some prior knowledge of Access
 End-user, not prog...
AdmissionsAIN1029
Approach:
SQL View Used in code; but helpful in
understanding what a query
does
Query Designer Use to co...
AIN102 Resources
 Manuals can be downloaded from SlideShare.
 Slides can be viewed on SlideShare…
http://www.slideshare....
1
AIN102
Access Query Design
Orange Coast Database Associates, Inc.
949-489-1472
http://www.dhdursoassociates.com
AIN102
A...
2
AIN102 Module 13 Admissions AIN102 Module 13
AIN102
 Session 1 – Review of Relational Databases
and Single Table Retrie...
3
AIN102 Module 15 Admissions AIN102 Module 15
Module 1
 Concepts
– Relational
databases
– SQL
– Access Query
Designer
 ...
4
AIN102 Module 17 Admissions AIN102 Module 17
Access Query Design and SQL
 Class database will be MS Access – the SQL
en...
5
AIN102 Module 19 Admissions AIN102 Module 19
Storage
 In Access one file with extension mdb. I.e
mydatabase.mdb.
 Data...
6
AIN102 Module 111 Admissions AIN102 Module 111
Relational Database Table
AIN102 Module 112 Admissions AIN102 Module 112
...
7
AIN102 Module 113 Admissions AIN102 Module 113
Database Relationships
AIN102 Module 114 Admissions AIN102 Module 114
App...
8
AIN102 Module 115 Admissions AIN102 Module 115
Query Types
 Select
 Action
– Update
– Delete
– Insert
– Maketable
 Sp...
9
AIN102 Module 117 Admissions AIN102 Module 117
MS Access DDL Queries
AIN102 Module 118 Admissions AIN102 Module 118
DCL
...
10
AIN102 Module 119 Admissions AIN102 Module 119
DML
 Data Manipulation Language (DML)
– Select Query
– Append Query
– U...
11
AIN102 Module 121 Admissions AIN102 Module 121
Other Access Queries
 Crosstab
 Pass Through – used to pass an SQL
sta...
12
AIN102 Module 123
Query Designer
Admissions AIN102 Module 123
Right click anywhere
for SQL View
Double click or drag
to...
13
AIN102 Module 125 Admissions AIN102 Module 125
MS Access SELECT Query
Drag and Drop
or double click
AIN102 Module 126 A...
14
AIN102 Module 127 Admissions AIN102 Module 127
Simple Select Query Results
AIN102 Module 128 Admissions AIN102 Module 1...
15
Admissions AIN102 Module 129
Comparison Operators
 < less than
 > greater than
 <= less than or equal
 >= greater t...
16
AIN102 Module 131 Admissions AIN102 Module 131
SELECT w/ WHERE
AIN102 Module 132 Admissions AIN102 Module 132
SELECT w/...
17
AIN102 Module 133 Admissions AIN102 Module 133
Complex Filters
Follows normal boolean logic
Select PatID, LastName, etc...
18
AIN102 Module 135 Admissions AIN102 Module 135
Select w/ Complex Where
AIN102 Module 136 Admissions AIN102 Module 136
C...
19
AIN102 Module 137 Admissions AIN102 Module 137
Special Operators
 LIKE
 IN
 BETWEEN
 IS NULL
AIN102 Module 138 Admi...
20
AIN102 Module 139 Admissions AIN102 Module 139
LIKE
Names ending in “S”
AIN102 Module 140 Admissions AIN102 Module 140
...
21
AIN102 Module 141 Admissions AIN102 Module 141
LIKE Results
AIN102 Module 142 Admissions AIN102 Module 142
IS NULL
Sele...
22
AIN102 Module 143 Admissions AIN102 Module 143
IS NULL
AIN102 Module 144 Admissions AIN102 Module 144
IS NULL
23
AIN102 Module 145 Admissions AIN102 Module 145
IS NULL Results
OK. Now try finding all patients with a blank address. W...
24
AIN102 Module 147 Admissions AIN102 Module 147
IN
AIN102 Module 148 Admissions AIN102 Module 148
IN
List of values
25
AIN102 Module 149 Admissions AIN102 Module 149
SELECT w/in IN Results
AIN102 Module 150 Admissions AIN102 Module 150
BE...
26
AIN102 Module 151 Admissions AIN102 Module 151
BETWEEN
AIN102 Module 152 Admissions AIN102 Module 152
BETWEEN Results
27
AIN102 Module 153 Admissions AIN102 Module 153
Removing Duplicates
SELECT DISTINCT PatNo
FROM admissions
List once each...
28
AIN102 Module 155 Admissions AIN102 Module 155
Distinct Results
W/out Distinct With Distinct
AIN102 Module 156 Admissio...
29
AIN102 Module 157 Admissions AIN102 Module 157
Sorting – ORDER BY
Select *
From patients
Where state in(“CA”, “OR”, “WA...
30
AIN102 Module 159 Admissions AIN102 Module 159
Sorting – ORDER BY
AIN102 Module 160 Admissions AIN102 Module 160
Sortin...
31
AIN102 Module 161 Admissions AIN102 Module 161
Query/SQL Exercises
 List all patient numbers, etc. for patients who co...
32
AIN102 Module 163 Admissions AIN102 Module 163
Calculated Fields
New column name
Follow name with “:”
AIN102 Module 164...
33
AIN102 Module 165 Admissions AIN102 Module 165
Calculated Fields - Results
AIN102 Module 166 Admissions AIN102 Module 1...
34
AIN102 Module 167 Admissions AIN102 Module 167
Concatenation
Sort, but don’t
show
AIN102 Module 168 Admissions AIN102 M...
35
AIN102 Module 169 Admissions AIN102 Module 169
Concatenation
Single column with full
name, sorted by last name
Tip: han...
36
AIN102 Module 171 Admissions AIN102 Module 171
End Session 1
Next session: multi-table
operations, subqueries, unions,
...
1
Admissions AIN102 Module 21
AIN102
Access Query Design
Module 2
P.O. Box 6142
Laguna Niguel, CA 92607
949-489-1472
http:...
2
Admissions AIN102 Module 23
String Manipulation
 Trim
 Substring
 UCase, LCase
 Left, Right
 See help for others
Ad...
3
Admissions AIN102 Module 25
String Manipulation
Admissions AIN102 Module 26
String Example
4
Admissions AIN102 Module 27
Date Functions
 Numerous date functions
– DatePart
– DateDiff
– DateAdd
– Etc.
 Often used...
5
Admissions AIN102 Module 29
DateDiff results
Note: Access has many date functions with many options.
Admissions AIN102 M...
6
Admissions AIN102 Module 211
Ex: Date Function – Month()
Admissions AIN102 Module 212
Result of Month Function
7
Admissions AIN102 Module 213
Summary Functions
 Count
 Sum
 Min
 Max
 Avg
 Often used in conjunction
with grouping...
8
Admissions AIN102 Module 215
Summary Functions - Syntax
Basic syntax:
SELECT function(column)
FROM table
WHERE filter-co...
9
Admissions AIN102 Module 217
Simple Column Summaries
Admissions AIN102 Module 218
Simple Record Count
10
Admissions AIN102 Module 219
“The COUNTS”
 Count(*) – counts records
 Count(fieldname) – counts non–null
occurrences ...
11
Admissions AIN102 Module 221
GROUP BY in Query Designer
Admissions AIN102 Module 222
GROUP BY Problem
Not an aggregate
...
12
Admissions AIN102 Module 223
Group By Results
Admissions AIN102 Module 224
Having
 Restricts the groups returned
 Ope...
13
Admissions AIN102 Module 225
Having – SQL View
 Restricts the groups returned
 Operates on the groups after they have...
14
Admissions AIN102 Module 227
HAVING Results
Admissions AIN102 Module 228
AIN102
Access Query Design
Part 2 – Joins, Sub...
15
Admissions AIN102 Module 229
Database Design
Diagram of the database for reference in joins
Admissions AIN102 Module 23...
16
Admissions AIN102 Module 231
Inner Join
 Pairs each row from first table with
corresponding row from second table over...
17
Admissions AIN102 Module 233
Table Aliases
 Shorthand name for a table
 Used in more complex queries
Select t.id, s.l...
18
Admissions AIN102 Module 235
Inner Join Query Design
Admissions AIN102 Module 236
Inner Join Results
19
Admissions AIN102 Module 237
Inner Join over Multiple columns
 Note that that the join condition can apply to
multiple...
20
Admissions AIN102 Module 239
3 Table Query Results
Admissions AIN102 Module 240
More on Aliases
 Can be set in Query
D...
21
Admissions AIN102 Module 241
Outer Joins
 Left
– selects all rows from the left or first table, even if no match
exist...
22
Admissions AIN102 Module 243
Left Outer Join
(Right click on relationship to edit)
Admissions AIN102 Module 244
Left Ou...
23
Admissions AIN102 Module 245
Left Outer Join Results
Admissions AIN102 Module 246
Subqueries
 One select statement emb...
24
Admissions AIN102 Module 247
Multi-valued Subquery
 A type of subquery that compares to a list
 Ex: find all diagnost...
25
Admissions AIN102 Module 249
Multi-valued Subquery Result
No one was ever admitted with these diagnoses.
Admissions AIN...
26
Admissions AIN102 Module 251
Single-valued Subquery
Subquery that returns a single value
Admissions AIN102 Module 252
S...
27
Admissions AIN102 Module 253
Queries using Queries
 Queries can be read just like a table
 Some are updateable
 Usin...
28
Admissions AIN102 Module 255
Queries using Queries Result
Admissions AIN102 Module 256
AIN102
Access Query Design
Part ...
29
Admissions AIN102 Module 257
Special Queries
 Most are “Action Queries”
 Reached through Query pull-down menu
Admissi...
30
Admissions AIN102 Module 259
Admissions Database
Diagram of the database for reference in part 3
Admissions AIN102 Modu...
31
Admissions AIN102 Module 261
Creating Action Queries
 First set up the select query based on the
table you are selecti...
32
Admissions AIN102 Module 263
Select Into…Maketable
New table
Existing table
Make Table
Admissions AIN102 Module 264
Arc...
33
Admissions AIN102 Module 265
Delete SQL
 Deletes one or more rows
Basic Syntax:
DELETE FROM table-name
WHERE filter-cr...
34
Admissions AIN102 Module 267
Delete SQL
Example: Delete all archived
admissions_history newer than 12/31/01
Admissions ...
35
Admissions AIN102 Module 269
More complex DELETE with FROM
clause
 Can delete based on matching
records in other table...
36
Admissions AIN102 Module 271
Single Row Insert
Basic Syntax:
Insert [into] table-name
Values (value-list)
Admissions AI...
37
Admissions AIN102 Module 273
Append Query (Multi-row insert)
Admissions AIN102 Module 274
Multi-row Insert SQL
Basic Sy...
38
Admissions AIN102 Module 275
Multi-row Insert (Append)
Add back archived admissions_history
newer than 12/31/01
Admissi...
39
Admissions AIN102 Module 277
Update
 Updates fields in an existing row
Basic Syntax:
UPDATE table-name
SET field1 = ne...
40
Admissions AIN102 Module 279
Update
 Increase the admission_history copays by 10%
(unrealistic to change history but w...
41
Admissions AIN102 Module 281
Unions
 Combines the results of two queries
– Ex: current records and history
 Tables mu...
42
Admissions AIN102 Module 283
Union Example
Admissions AIN102 Module 284
Union Result
43
Admissions AIN102 Module 285
End Session 2
End of Class!
Notes
Admissions AIN102 Module 286
44
Notes
Admissions AIN102 Module 287
Notes
Admissions AIN102 Module 288
Accelerated Computer Training for Working Professionals
Orange Coast Database Associates Course (800)355-9855 or http://oc...
Upcoming SlideShare
Loading in...5
×

AIN102 Microsoft Access Queries

539

Published on

Microsft Access queries course. Covers basic select quesries, filter conditions, special operators, joins, subqueries, unions, crosstabs and functions.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
539
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "AIN102 Microsoft Access Queries"

  1. 1. Accelerated Computer Training for Working Professionals Admissions Case Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Classes custom designed forWorking Professionals http://www.dhdursoassociates.com San Juan Capistrano, CA (800)355-9855 AIN102 Microsoft Access Queries
  2. 2. Admissions AIN1021 AIN102 – Access Query Design Introduction to Access Queries for experienced Windows users P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com AdmissionsAIN1022 AIN102 Contact Information P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com slides.1@dhdursoassociates.com Copyright 2001-20011 All rights reserved.
  3. 3. AIN102 Notes  This course is based on an earlier course, SQL200A, which was focused on using SQL in the Access environment. As such the screenshots still reflect SQL view as well the Query Designer view. This is actually a plus. AdmissionsAIN1023 AdmissionsAIN1024  Introduction (s)  Facilities  Course Packet (May vary by course or class) – Student questionnaire in front – Collaterals (Map, Catalog, Etc.) – PowerPoint handouts for all sessions – Evaluation form in back – Training Certificate AIN102 – Access Query Design
  4. 4. AdmissionsAIN1025 Orange Coast Database Associates  Loose association of local University instructors and IT Professionals.  Started in 2004  Training – Open Enrollment (public classes in San Juan Capistrano using standardized material) – On-site (custom classes for groups) – Private desk side training for individuals  Consulting & Programming (incl. offshore) AdmissionsAIN1026
  5. 5. AdmissionsAIN1027  Quick pace for experienced windows users  Assumes some prior knowledge of Access  End-user, not programmer, oriented  Somewhat structured towards personal use for analysis and reporting AIN102 – Access Query Design AdmissionsAIN1028  Select Queries – Filter Criteria – Calculations – Joins – Grouping  Action Queries – Maketable – Append – Update – Delete  Union Course Topics: AIN102 – Access Query Design
  6. 6. AdmissionsAIN1029 Approach: SQL View Used in code; but helpful in understanding what a query does Query Designer Use to construct queries Access has many ways to accomplish tasks. Queries can be constructed using SQL View or the Query Designer. This course focuses on the latter. AIN102 – Access Query Design AdmissionsAIN10210  2 Sessions  Lecture  Demo  Extensive student “hands-on”  Exercises are cumulative – later examples build on objects created earlier Course Format: AIN102 – Access Query Design
  7. 7. AIN102 Resources  Manuals can be downloaded from SlideShare.  Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases AdmissionsAIN10211 AdmissionsAIN10212  Session 1 – access-qdes-s1-ad.ppt  Session 2 – access-qdes-s2-ad.ppt Course Schedule (3 hour sessions): AIN102 – Access Query Design
  8. 8. 1 AIN102 Access Query Design Orange Coast Database Associates, Inc. 949-489-1472 http://www.dhdursoassociates.com AIN102 Access Query Design Module 1 – Relational Database Background, Basic Single Table Retrieval Operations
  9. 9. 2 AIN102 Module 13 Admissions AIN102 Module 13 AIN102  Session 1 – Review of Relational Databases and Single Table Retrieval, Functions and Operators  Session 2 – Multi-table retrieval, Subqueries, Unions, Action Queries, Indexes AIN102 Module 14 Admissions AIN102 Module 14 AIN102  Lecture/discussion with demonstrations – Mostly practical, but a dose of “theory”  Assumes some familiarity with MS Access  Hands-on  Exercises (Mostly cumulative)  About 3-4 hours per module  Frequent breaks
  10. 10. 3 AIN102 Module 15 Admissions AIN102 Module 15 Module 1  Concepts – Relational databases – SQL – Access Query Designer  SELECT Query – Basic – Filter criteria – Complex criteria • Field Operations • Functions • Operators • Calculated fields • Concatenation AIN102 Module 16 Admissions AIN102 Module 16 Microsoft Access Query Design  Course focus is Microsoft Access Query Design  Queries are widely used for: – Database administration – Enterprise application development – Data driven web sites – Retrieving and modifying data in MS Access databases  A foundation skill for eBusiness and almost all major business applications that use relational databases
  11. 11. 4 AIN102 Module 17 Admissions AIN102 Module 17 Access Query Design and SQL  Class database will be MS Access – the SQL engine Access uses is called JET  A basic knowledge of Access is assumed  Focus of this class will be on the query designer, with SQL used from time-to-time to explain concepts AIN102 Module 18 Admissions AIN102 Module 18 Relational Database Basics  Storage  Databases  Tables  Rows  Columns  Indexes  Views  SQL interface
  12. 12. 5 AIN102 Module 19 Admissions AIN102 Module 19 Storage  In Access one file with extension mdb. I.e mydatabase.mdb.  Database splitter can be used to split it into two parts – a front end and a back end.  Interface to physical storage via the “JET” database engine. This is the behind-the scenes-workhorse. AIN102 Module 110 Admissions AIN102 Module 110 Databases  In the Access world refers to all objects stored in the mdb. – Tables – Queries – From – Reports – Macros – Code modules
  13. 13. 6 AIN102 Module 111 Admissions AIN102 Module 111 Relational Database Table AIN102 Module 112 Admissions AIN102 Module 112 Sample Access Database
  14. 14. 7 AIN102 Module 113 Admissions AIN102 Module 113 Database Relationships AIN102 Module 114 Admissions AIN102 Module 114 Approaching Query Design  Try to build queries a step at a time  Save existing queries that are working as you like; modify them with a new name when building a modified query  Try looking at the generated SQL code from time-to-time
  15. 15. 8 AIN102 Module 115 Admissions AIN102 Module 115 Query Types  Select  Action – Update – Delete – Insert – Maketable  Special use queries – Data Definition Language (DDL) – Union – SQL pass-through AIN102 Module 116 Admissions AIN102 Module 116 DDL  Data definition language (DDL) to create and modify tables, etc. – Create, alter, drop, etc. – Must be “hand-coded” in Access – But can be useful for database administration  Done in MS Access via SQL Specific Queries – Will not be covered in the course
  16. 16. 9 AIN102 Module 117 Admissions AIN102 Module 117 MS Access DDL Queries AIN102 Module 118 Admissions AIN102 Module 118 DCL  Data Control Language (DCL) to control user rights, etc. – Grant – Revoke – Constraints  Used primarily in enterprise databases  Can be done in Access but not covered in this course
  17. 17. 10 AIN102 Module 119 Admissions AIN102 Module 119 DML  Data Manipulation Language (DML) – Select Query – Append Query – Update Query – Delete Query – Maketable Query (a special type of Select) – Union Query (really combined Selects)  MS Access Query Designer will create these statements “behind the scenes” AIN102 Module 120 Admissions AIN102 Module 120 MS Access Action Queries
  18. 18. 11 AIN102 Module 121 Admissions AIN102 Module 121 Other Access Queries  Crosstab  Pass Through – used to pass an SQL statement directly to a back end database w/out processing by Access AIN102 Module 122 Admissions AIN102 Module 122 Sample Database  Before we continue…  Load the sample database if you haven’t already
  19. 19. 12 AIN102 Module 123 Query Designer Admissions AIN102 Module 123 Right click anywhere for SQL View Double click or drag to add a field Double click to add a table AIN102 Module 124 Admissions AIN102 Module 124 SELECT Queries Basic Syntax: Select column-list or * From table-list * Means all columns
  20. 20. 13 AIN102 Module 125 Admissions AIN102 Module 125 MS Access SELECT Query Drag and Drop or double click AIN102 Module 126 Admissions AIN102 Module 126 MS Access SELECT SQL
  21. 21. 14 AIN102 Module 127 Admissions AIN102 Module 127 Simple Select Query Results AIN102 Module 128 Admissions AIN102 Module 128 SELECT w/ Where Filters retrieved rows Syntax: SELECT column-list FROM table-list WHERE selection-criteria
  22. 22. 15 Admissions AIN102 Module 129 Comparison Operators  < less than  > greater than  <= less than or equal  >= greater than or equal  = equal  <> not equal AIN102 Module 130 Admissions AIN102 Module 130 SELECT w/ WHERE
  23. 23. 16 AIN102 Module 131 Admissions AIN102 Module 131 SELECT w/ WHERE AIN102 Module 132 Admissions AIN102 Module 132 SELECT w/ WHERE Results
  24. 24. 17 AIN102 Module 133 Admissions AIN102 Module 133 Complex Filters Follows normal boolean logic Select PatID, LastName, etc… From patients Where (LastName = “Thomas” or birthdate < #1/1/95#) and city = “Corona”) AIN102 Module 134 Admissions AIN102 Module 134 Complex Filter AND
  25. 25. 18 AIN102 Module 135 Admissions AIN102 Module 135 Select w/ Complex Where AIN102 Module 136 Admissions AIN102 Module 136 Complex Filter Results
  26. 26. 19 AIN102 Module 137 Admissions AIN102 Module 137 Special Operators  LIKE  IN  BETWEEN  IS NULL AIN102 Module 138 Admissions AIN102 Module 138 Like (“Wild Card Matches”)  Where customer_last_name like “Jo*” – *= string of characters  Where customer_last_name like “Jo?” – ? = exactly one character Access actually allows more sophisticated Unix style search patterns as well – see help.
  27. 27. 20 AIN102 Module 139 Admissions AIN102 Module 139 LIKE Names ending in “S” AIN102 Module 140 Admissions AIN102 Module 140 LIKE Names ending in “S”
  28. 28. 21 AIN102 Module 141 Admissions AIN102 Module 141 LIKE Results AIN102 Module 142 Admissions AIN102 Module 142 IS NULL Select columnns From clients Where zip IS NULL SQL (including JET) uses three valued logic. Must use IS NULL to test for unknowns. A null is NOT the same as blank or empty.
  29. 29. 22 AIN102 Module 143 Admissions AIN102 Module 143 IS NULL AIN102 Module 144 Admissions AIN102 Module 144 IS NULL
  30. 30. 23 AIN102 Module 145 Admissions AIN102 Module 145 IS NULL Results OK. Now try finding all patients with a blank address. Was the above row retrieved? Why or why not? AIN102 Module 146 Admissions AIN102 Module 146 IN Select * From patients Where lastname IN (“Smith”, “Thomas”, “Juarez”) Note: what is inside parentheses is a list. Later we will replace the list with a subquery which generates the list items.
  31. 31. 24 AIN102 Module 147 Admissions AIN102 Module 147 IN AIN102 Module 148 Admissions AIN102 Module 148 IN List of values
  32. 32. 25 AIN102 Module 149 Admissions AIN102 Module 149 SELECT w/in IN Results AIN102 Module 150 Admissions AIN102 Module 150 BETWEEN SELECT * FROM admissions WHERE admit_date BETWEEN #10/1/2001# and #12/31/2001# Note: between is inclusive
  33. 33. 26 AIN102 Module 151 Admissions AIN102 Module 151 BETWEEN AIN102 Module 152 Admissions AIN102 Module 152 BETWEEN Results
  34. 34. 27 AIN102 Module 153 Admissions AIN102 Module 153 Removing Duplicates SELECT DISTINCT PatNo FROM admissions List once each employee who has a labor ticket Removes duplicate rows from result set AIN102 Module 154 Admissions AIN102 Module 154 DISTINCT
  35. 35. 28 AIN102 Module 155 Admissions AIN102 Module 155 Distinct Results W/out Distinct With Distinct AIN102 Module 156 Admissions AIN102 Module 156 Sorting – ORDER BY DESC will sort in descending order Basic syntax: SELECT column list FROM table list WHERE selection criteria ORDER BY column list [DESC]
  36. 36. 29 AIN102 Module 157 Admissions AIN102 Module 157 Sorting – ORDER BY Select * From patients Where state in(“CA”, “OR”, “WA”) Order by birthdate desc Example: List patients in Pacific Coast sorted by youngest to oldest AIN102 Module 158 Admissions AIN102 Module 158 Sorting – ORDER BY Note: modify the “select_in” query
  37. 37. 30 AIN102 Module 159 Admissions AIN102 Module 159 Sorting – ORDER BY AIN102 Module 160 Admissions AIN102 Module 160 Sorting – Results
  38. 38. 31 AIN102 Module 161 Admissions AIN102 Module 161 Query/SQL Exercises  List all patient numbers, etc. for patients who co- paid more than $20; sort by co-pay amount, largest first  List diagnostics that contain b, d, or q in the diagnostic code  List the patient id’s for admissions in the fall (Sep, Oct, Nov) of 2001 and 2002 AIN102 Module 162 Admissions AIN102 Module 162 Calculated Fields Example: Select a + b as result Syntax: SELECT expression AS new column name Result is a “column alias”
  39. 39. 32 AIN102 Module 163 Admissions AIN102 Module 163 Calculated Fields New column name Follow name with “:” AIN102 Module 164 Admissions AIN102 Module 164 Calculated Fields
  40. 40. 33 AIN102 Module 165 Admissions AIN102 Module 165 Calculated Fields - Results AIN102 Module 166 Admissions AIN102 Module 166 Concatenation  “Adds” two character fields using “&” symbol  Useful for formatting names, addresses, etc.  Example: – Select firstname & “ “ & lastname as fullname
  41. 41. 34 AIN102 Module 167 Admissions AIN102 Module 167 Concatenation Sort, but don’t show AIN102 Module 168 Admissions AIN102 Module 168 Concatenation
  42. 42. 35 AIN102 Module 169 Admissions AIN102 Module 169 Concatenation Single column with full name, sorted by last name Tip: handy for reports AIN102 Module 170 Admissions AIN102 Module 170 SQL Exercises  List all the patients with a “fullname” field; sorted by last name. Add a calculated field which shows their loaded copay (copay *1.5). Show the fullname, but not the individual names.
  43. 43. 36 AIN102 Module 171 Admissions AIN102 Module 171 End Session 1 Next session: multi-table operations, subqueries, unions, action queries, indexes
  44. 44. 1 Admissions AIN102 Module 21 AIN102 Access Query Design Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com Admissions AIN102 Module 22 Module 2  Part 1 – Miscellaneous Functions  Strings  Dates – Summaries (Grouping)  Part 2 – Joins, subqueries – Inner join – Outer joins – Subqueries  Multi-valued  Single-valued • Part 3 – Unions, Action Queries, Indexes • Unions • Maketable Query • Delete • Append • Update • Indexes
  45. 45. 2 Admissions AIN102 Module 23 String Manipulation  Trim  Substring  UCase, LCase  Left, Right  See help for others Admissions AIN102 Module 24 String Manipulation
  46. 46. 3 Admissions AIN102 Module 25 String Manipulation Admissions AIN102 Module 26 String Example
  47. 47. 4 Admissions AIN102 Module 27 Date Functions  Numerous date functions – DatePart – DateDiff – DateAdd – Etc.  Often used: – Year – Month  Ex: where year(birthdate) = 1999 Admissions AIN102 Module 28 DateDiff  How long did patients stay in weeks?
  48. 48. 5 Admissions AIN102 Module 29 DateDiff results Note: Access has many date functions with many options. Admissions AIN102 Module 210 Ex: Date Function – Month()
  49. 49. 6 Admissions AIN102 Module 211 Ex: Date Function – Month() Admissions AIN102 Module 212 Result of Month Function
  50. 50. 7 Admissions AIN102 Module 213 Summary Functions  Count  Sum  Min  Max  Avg  Often used in conjunction with grouping Admissions AIN102 Module 214 Summary Functions in Access Click the sum symbol Adds a total row
  51. 51. 8 Admissions AIN102 Module 215 Summary Functions - Syntax Basic syntax: SELECT function(column) FROM table WHERE filter-condition GROUP BY column-list HAVING group-filter Group by all columns to left of one(s) you to want aggregate Admissions AIN102 Module 216 Simple Column Summaries This query counts patient admissions In the year 2001
  52. 52. 9 Admissions AIN102 Module 217 Simple Column Summaries Admissions AIN102 Module 218 Simple Record Count
  53. 53. 10 Admissions AIN102 Module 219 “The COUNTS”  Count(*) – counts records  Count(fieldname) – counts non–null occurrences of field name  Count (distinct fieldname) – counts distinct occurrences, but not supported in access Admissions AIN102 Module 220 Grouping  Organizes results into summary rows, one per group  Groups can have sub groups which have sub groups and so on….
  54. 54. 11 Admissions AIN102 Module 221 GROUP BY in Query Designer Admissions AIN102 Module 222 GROUP BY Problem Not an aggregate or group You will see this error a lot. Not to worry. It happens to everyone!
  55. 55. 12 Admissions AIN102 Module 223 Group By Results Admissions AIN102 Module 224 Having  Restricts the groups returned  Operates on the groups after they have been formed HAVING (((Admissions.Diag_Code) Like "a*"))
  56. 56. 13 Admissions AIN102 Module 225 Having – SQL View  Restricts the groups returned  Operates on the groups after they have been formed Admissions AIN102 Module 226 HAVING
  57. 57. 14 Admissions AIN102 Module 227 HAVING Results Admissions AIN102 Module 228 AIN102 Access Query Design Part 2 – Joins, Subqueries
  58. 58. 15 Admissions AIN102 Module 229 Database Design Diagram of the database for reference in joins Admissions AIN102 Module 230 Joins  Used to combine columns from more than one table  Several types – Inner – Outer  Left  Right – Others (not covered)  Full Outer (Not supported in Access)  Cross  Self  Non equal
  59. 59. 16 Admissions AIN102 Module 231 Inner Join  Pairs each row from first table with corresponding row from second table over the “join column” or “linking column”  The result only contains rows where there is a match over the join column in both tables  The default join in most databases Admissions AIN102 Module 232 Inner Join Syntax Basic SQL 92 Syntax: SELECT column-list FROM table1 [AS alias] INNER JOIN table2 [AS alias] ON join-condition
  60. 60. 17 Admissions AIN102 Module 233 Table Aliases  Shorthand name for a table  Used in more complex queries Select t.id, s.lname From _traveler as t Inner join xLU_Staff as s On t.staffid = s.staffid Table alias Admissions AIN102 Module 234 Inner Join Basic SQL Example Basic Example: Add patient names to admissions data Two join tables Join condition
  61. 61. 18 Admissions AIN102 Module 235 Inner Join Query Design Admissions AIN102 Module 236 Inner Join Results
  62. 62. 19 Admissions AIN102 Module 237 Inner Join over Multiple columns  Note that that the join condition can apply to multiple columns if desired  Used with composite keys Select …. From tablea as ta Inner join tableb as tb On ta.key1 = tb.key1 And ta.key2 = tb.key2 Admissions AIN102 Module 238 Joining More than Two Tables  Can join several tables in one select  Try to limit to three or four  Demonstration example
  63. 63. 20 Admissions AIN102 Module 239 3 Table Query Results Admissions AIN102 Module 240 More on Aliases  Can be set in Query Designer, too, by right clicking on a table  Useful for debugging top level queries as we shall see later
  64. 64. 21 Admissions AIN102 Module 241 Outer Joins  Left – selects all rows from the left or first table, even if no match exists in the other table – Widely used in commercial practice, esp. for reporting  Right – same idea but all rows from right table  Full – all rows from both tables; not supported in Access Admissions AIN102 Module 242 Left Outer Join Basic SQL 92 Syntax: SELECT column-list FROM table1 LEFT JOIN table2 ON join-condition
  65. 65. 22 Admissions AIN102 Module 243 Left Outer Join (Right click on relationship to edit) Admissions AIN102 Module 244 Left Outer Join  Modify your prior inner join to use a left join  Save as qryLeft_Admit  Now run both the inner and left joins  What is the difference?
  66. 66. 23 Admissions AIN102 Module 245 Left Outer Join Results Admissions AIN102 Module 246 Subqueries  One select statement embedded in another  Can be nested multiple levels deep  In Access query designer can be placed in criteria row or field row
  67. 67. 24 Admissions AIN102 Module 247 Multi-valued Subquery  A type of subquery that compares to a list  Ex: find all diagnostic codes with no admissions  Commonly encountered in commercial practice Admissions AIN102 Module 248 Multi-valued Subquery  Place subquery in criteria row
  68. 68. 25 Admissions AIN102 Module 249 Multi-valued Subquery Result No one was ever admitted with these diagnoses. Admissions AIN102 Module 250 Single-valued Subquery Subquery that returns a single value Find all admissions with stays greater than the average stay
  69. 69. 26 Admissions AIN102 Module 251 Single-valued Subquery Subquery that returns a single value Admissions AIN102 Module 252 Single-valued Subquery Result
  70. 70. 27 Admissions AIN102 Module 253 Queries using Queries  Queries can be read just like a table  Some are updateable  Using a query as a source for another query very common in Access – especially for complicated queries  In a “back end” database such as Oracle or SQL Sever you would use a View Admissions AIN102 Module 254 Queries using Queries Filter the prior query ( a little on the complicated side) for the year 2001
  71. 71. 28 Admissions AIN102 Module 255 Queries using Queries Result Admissions AIN102 Module 256 AIN102 Access Query Design Part 3 – Unions, Action Queries D. H. D’Urso and Associates 949-489-1472 http://www.dhdursoassociates.com
  72. 72. 29 Admissions AIN102 Module 257 Special Queries  Most are “Action Queries”  Reached through Query pull-down menu Admissions AIN102 Module 258 Special Queries  Special action queries have distinct symbols
  73. 73. 30 Admissions AIN102 Module 259 Admissions Database Diagram of the database for reference in part 3 Admissions AIN102 Module 260 Data Modification Queries SQL Action Query INSERT Append UPDATE Update DELETE Delete SELECT…INTO Maketable
  74. 74. 31 Admissions AIN102 Module 261 Creating Action Queries  First set up the select query based on the table you are selecting from  Test select query  Then convert to action query  When you run an action query there is no result displayed – must look at the affected table to see the result Admissions AIN102 Module 262 Select Into…Maketable  Creates a new table “on the fly”
  75. 75. 32 Admissions AIN102 Module 263 Select Into…Maketable New table Existing table Make Table Admissions AIN102 Module 264 Archived Labor History
  76. 76. 33 Admissions AIN102 Module 265 Delete SQL  Deletes one or more rows Basic Syntax: DELETE FROM table-name WHERE filter-criteria Admissions AIN102 Module 266 Delete Example: Delete all archived records newer than 12/31/2001
  77. 77. 34 Admissions AIN102 Module 267 Delete SQL Example: Delete all archived admissions_history newer than 12/31/01 Admissions AIN102 Module 268 Archive after DELETE
  78. 78. 35 Admissions AIN102 Module 269 More complex DELETE with FROM clause  Can delete based on matching records in other tables  Uses from for criteria instead of where – uses the matched rows as an implicit filter condition  Covered in advanced class Admissions AIN102 Module 270 Append Query (SQL Insert)  Adds new rows to an existing table  Two forms: – Single Row – Multi-Row
  79. 79. 36 Admissions AIN102 Module 271 Single Row Insert Basic Syntax: Insert [into] table-name Values (value-list) Admissions AIN102 Module 272 Single Row Insert Basic Example: INSERT into xLU_BuildStyle values(6, “Thick”) Not really used this way in MS Access. Typical MS Access use would be Insert…Select as shown in following slides.
  80. 80. 37 Admissions AIN102 Module 273 Append Query (Multi-row insert) Admissions AIN102 Module 274 Multi-row Insert SQL Basic Syntax: INSERT [INTO] table-name SELECT select-statement
  81. 81. 38 Admissions AIN102 Module 275 Multi-row Insert (Append) Add back archived admissions_history newer than 12/31/01 Admissions AIN102 Module 276 Append (Insert) Results
  82. 82. 39 Admissions AIN102 Module 277 Update  Updates fields in an existing row Basic Syntax: UPDATE table-name SET field1 = new value, field2 = new value,… WHERE selection-criteria Admissions AIN102 Module 278 Update in Query Designer New value
  83. 83. 40 Admissions AIN102 Module 279 Update  Increase the admission_history copays by 10% (unrealistic to change history but we don’t want to step on our good tables) Admissions AIN102 Module 280 Update Results New values Remember: you have to look at the affected table. No “result” is displayed.
  84. 84. 41 Admissions AIN102 Module 281 Unions  Combines the results of two queries – Ex: current records and history  Tables must be union compatible (at least in theory!)  There is no designer in Access; must be done in SQL view Admissions AIN102 Module 282 Union Syntax SELECT column-list FROM table1 UNION [ALL] SELECT same-columns FROM table2
  85. 85. 42 Admissions AIN102 Module 283 Union Example Admissions AIN102 Module 284 Union Result
  86. 86. 43 Admissions AIN102 Module 285 End Session 2 End of Class! Notes Admissions AIN102 Module 286
  87. 87. 44 Notes Admissions AIN102 Module 287 Notes Admissions AIN102 Module 288
  88. 88. Accelerated Computer Training for Working Professionals Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Computer Training, Programming & Consulting 32422 Alipaz St., Suite A-15 San Juan Capistrano, CA (800)355-9855 (Toll Free) | (949)489-1472 (Direct) | (949)485-6284 (Fax) http://www.dhdursoassociates.com | sales@dhdursoassociates.com Accelerated Computer Training

×