AIN102 Access Query Design Module 2 Admissions AIN102  Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com
AIN102 Contact Information Admissions AIN102  Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address]   Copyright 2001-20011 All rights reserved.
AIN102 Notes This course is based on an earlier course, SQL200A, which was focused on using SQL in the Access environment. As such some screenshots still reflect SQL view as well the Query Designer view. This course is taught regularly in San Juan Capistrano, California. Private classes can be arranged here or at your facility. See contact information on the previous slide. Admissions AIN102  Module 2
AIN102 Resources The admissions starter database can be downloaded from  box.net . Manuals can be downloaded from the SlideShare link below. Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases   Admissions AIN102  Module 2
Module 2 Part 1 Miscellaneous Functions Strings Dates Summaries (Grouping) Part 2 – Joins, subqueries Inner join Outer joins Subqueries Multi-valued Single-valued Admissions AIN102  Module 2 Part 3 – Unions, Action Queries, Indexes Unions Maketable Query Delete Append Update Indexes
String Manipulation Trim Substring UCase, LCase Left, Right See help for others Admissions AIN102  Module 2
String Manipulation Admissions AIN102  Module 2
String Manipulation Admissions AIN102  Module 2
String Example Admissions AIN102  Module 2
Date Functions Numerous date functions DatePart DateDiff DateAdd Etc. Often used: Year Month Ex: where year(birthdate) = 1999 Admissions AIN102  Module 2
DateDiff How long did patients stay in weeks? Admissions AIN102  Module 2
DateDiff results Admissions AIN102  Module 2 Note: Access has  many date functions with many options.
Ex: Date Function – Month() Admissions AIN102  Module 2
Ex: Date Function – Month() Admissions AIN102  Module 2
Result of Month Function Admissions AIN102  Module 2
When you want to know more AIN102D Date functions 3 hour workshop AIN102S String functions 3 hour workshop See SlideShare for available material. Admissions AIN102  Module 2
Summary Functions Count Sum Min Max Avg Often used in conjunction with grouping Admissions AIN102  Module 2
Summary Functions in Access Admissions AIN102  Module 2 Click the sum symbol Adds a total row
Summary Functions - Syntax Admissions AIN102  Module 2 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
Simple Column Summaries Admissions AIN102  Module 2 This query counts patient admissions In the year 2001
Simple Column Summaries Admissions AIN102  Module 2
Simple Record Count Admissions AIN102  Module 2
“ 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 2
Grouping Organizes results into summary rows, one per group Groups can have sub groups which have sub groups and so on…. Admissions AIN102  Module 2
GROUP BY in Query Designer Admissions AIN102  Module 2
GROUP BY Problem Admissions AIN102  Module 2 Not an aggregate or group You will see this error a lot. Not to worry. It happens to everyone!
Group By Results Admissions AIN102  Module 2
Having Restricts the  groups  returned Operates on the groups  after  they have been formed Admissions AIN102  Module 2 HAVING (((Admissions.Diag_Code) Like "a*"))
Having – SQL View Restricts the  groups  returned Operates on the groups  after  they have been formed Admissions AIN102  Module 2
HAVING Admissions AIN102  Module 2
HAVING Results Admissions AIN102  Module 2
AIN102 Access Query Design Part 2 – Joins, Subqueries Admissions AIN102  Module 2
Admissions AIN102  Module 2 Database Design Diagram of the database for reference in joins
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 Admissions AIN102  Module 2
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 2
Inner Join Syntax Admissions AIN102  Module 2 Basic SQL 92 Syntax: SELECT   column-list FROM   table1  [ AS   alias ] INNER JOIN   table2  [ AS   alias ] ON   join-condition
Table Aliases Shorthand name for a table Used in more complex queries Admissions AIN102  Module 2 Select t.id, s.lname From _traveler as t Inner join xLU_Staff as s On t.staffid  =  s.staffid Table alias
Inner Join Basic SQL Example Admissions AIN102  Module 2 Basic Example:  Add patient names to admissions data Two join tables Join condition
Inner Join Query Design Admissions AIN102  Module 2
Inner Join Results Admissions AIN102  Module 2
Inner Join over Multiple columns Note that that the join condition can apply to multiple columns if desired Used with composite keys Admissions AIN102  Module 2 Select …. From tablea as ta Inner join tableb as tb On ta.key1 = tb.key1 And ta.key2 = tb.key2
Joining More than Two Tables Can join several tables in one select Try to limit to three or four Demonstration example Admissions AIN102  Module 2
3 Table Query Results Admissions AIN102  Module 2
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 Admissions AIN102  Module 2
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 2
Left Outer Join Admissions AIN102  Module 2 Basic SQL 92 Syntax: SELECT   column-list FROM   table1 LEFT JOIN   table2 ON   join-condition
Left Outer Join (Right click on relationship to edit) Admissions AIN102  Module 2
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? Admissions AIN102  Module 2
Left Outer Join Results Admissions AIN102  Module 2
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 Admissions AIN102  Module 2
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 2
Multi-valued Subquery Place subquery in criteria row Admissions AIN102  Module 2
Multi-valued Subquery Result Admissions AIN102  Module 2 No one was ever admitted with these diagnoses.
Single-valued Subquery Admissions AIN102  Module 2 Subquery that returns a single value Find all admissions with stays greater than the average stay
Single-valued Subquery Admissions AIN102  Module 2 Subquery that returns a single value
Single-valued Subquery Result Admissions AIN102  Module 2
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 2
Queries using Queries Admissions AIN102  Module 2 Filter the prior query ( a little on the complicated side) for the year 2001
Queries using Queries Result Admissions AIN102  Module 2
AIN102 Access Query Design Part 3 – Unions, Action Queries Admissions AIN102  Module 2 D. H. D’Urso and Associates 949-489-1472 http://www.dhdursoassociates.com
Special Queries Most are “Action Queries” Reached through Query pull-down menu Admissions AIN102  Module 2
Special Queries Special action queries have distinct symbols Admissions AIN102  Module 2
Admissions Database Admissions AIN102  Module 2 Diagram of the database for reference in part 3
Data Modification Queries Admissions AIN102  Module 2 SQL Action Query INSERT Append UPDATE Update DELETE Delete SELECT…INTO Maketable
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 2
Select Into…Maketable Creates a new table “on the fly” Admissions AIN102  Module 2
Select Into…Maketable Admissions AIN102  Module 2 New table Existing table Make Table
Archived Labor History  Admissions AIN102  Module 2
Delete SQL Deletes one or more rows Admissions AIN102  Module 2 Basic Syntax: DELETE FROM   table-name WHERE   filter-criteria
Delete Admissions AIN102  Module 2 Example: Delete all archived records newer than 12/31/2001
Delete SQL Admissions AIN102  Module 2 Example: Delete all archived admissions_history newer than 12/31/01
Archive after DELETE Admissions AIN102  Module 2
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 2
Append Query (SQL Insert) Adds new rows to an existing table Two forms: Single Row Multi-Row Admissions AIN102  Module 2
Single Row Insert Admissions AIN102  Module 2 Basic Syntax: Insert  [ into ]  table-name Values  ( value-list )
Single Row Insert Admissions AIN102  Module 2 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.
Append Query (Multi-row insert) Admissions AIN102  Module 2
Multi-row Insert SQL Admissions AIN102  Module 2 Basic Syntax: INSERT  [ INTO ]  table-name SELECT   select-statement
Multi-row Insert (Append) Admissions AIN102  Module 2 Add back archived admissions_history newer than 12/31/01
Append (Insert) Results Admissions AIN102  Module 2
Update Updates fields in an existing row Admissions AIN102  Module 2 Basic Syntax: UPDATE   table-name SET   field1  =  new value ,  field2  =  new value ,… WHERE  selection-criteria
Update in Query Designer Admissions AIN102  Module 2 New value
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 2
Update Results Admissions AIN102  Module 2 New values Remember: you have to look at the affected table. No “result” is displayed.
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 2
Union Syntax Admissions AIN102  Module 2 SELECT   column-list FROM   table1 UNION  [ ALL ] SELECT   same-columns FROM   table2
Union Example Admissions AIN102  Module 2
Union Result Admissions AIN102  Module 2
When you want to know more ½ day workshops. 1 day SQL200A Microsoft Access SQL Class. Admissions AIN102  Module 2
End Session 2 Admissions AIN102  Module 2 End of Class!
Notes Admissions AIN102  Module 2
Notes Admissions AIN102  Module 2

AIN102.2 Microsoft Access Queries

  • 1.
    AIN102 Access QueryDesign Module 2 Admissions AIN102 Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com
  • 2.
    AIN102 Contact InformationAdmissions AIN102 Module 2 P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com [email_address] Copyright 2001-20011 All rights reserved.
  • 3.
    AIN102 Notes Thiscourse is based on an earlier course, SQL200A, which was focused on using SQL in the Access environment. As such some screenshots still reflect SQL view as well the Query Designer view. This course is taught regularly in San Juan Capistrano, California. Private classes can be arranged here or at your facility. See contact information on the previous slide. Admissions AIN102 Module 2
  • 4.
    AIN102 Resources Theadmissions starter database can be downloaded from box.net . Manuals can be downloaded from the SlideShare link below. Slides can be viewed on SlideShare… http://www.slideshare.net/OCDatabases Admissions AIN102 Module 2
  • 5.
    Module 2 Part1 Miscellaneous Functions Strings Dates Summaries (Grouping) Part 2 – Joins, subqueries Inner join Outer joins Subqueries Multi-valued Single-valued Admissions AIN102 Module 2 Part 3 – Unions, Action Queries, Indexes Unions Maketable Query Delete Append Update Indexes
  • 6.
    String Manipulation TrimSubstring UCase, LCase Left, Right See help for others Admissions AIN102 Module 2
  • 7.
  • 8.
  • 9.
  • 10.
    Date Functions Numerousdate functions DatePart DateDiff DateAdd Etc. Often used: Year Month Ex: where year(birthdate) = 1999 Admissions AIN102 Module 2
  • 11.
    DateDiff How longdid patients stay in weeks? Admissions AIN102 Module 2
  • 12.
    DateDiff results AdmissionsAIN102 Module 2 Note: Access has many date functions with many options.
  • 13.
    Ex: Date Function– Month() Admissions AIN102 Module 2
  • 14.
    Ex: Date Function– Month() Admissions AIN102 Module 2
  • 15.
    Result of MonthFunction Admissions AIN102 Module 2
  • 16.
    When you wantto know more AIN102D Date functions 3 hour workshop AIN102S String functions 3 hour workshop See SlideShare for available material. Admissions AIN102 Module 2
  • 17.
    Summary Functions CountSum Min Max Avg Often used in conjunction with grouping Admissions AIN102 Module 2
  • 18.
    Summary Functions inAccess Admissions AIN102 Module 2 Click the sum symbol Adds a total row
  • 19.
    Summary Functions -Syntax Admissions AIN102 Module 2 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
  • 20.
    Simple Column SummariesAdmissions AIN102 Module 2 This query counts patient admissions In the year 2001
  • 21.
    Simple Column SummariesAdmissions AIN102 Module 2
  • 22.
    Simple Record CountAdmissions AIN102 Module 2
  • 23.
    “ 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 2
  • 24.
    Grouping Organizes resultsinto summary rows, one per group Groups can have sub groups which have sub groups and so on…. Admissions AIN102 Module 2
  • 25.
    GROUP BY inQuery Designer Admissions AIN102 Module 2
  • 26.
    GROUP BY ProblemAdmissions AIN102 Module 2 Not an aggregate or group You will see this error a lot. Not to worry. It happens to everyone!
  • 27.
    Group By ResultsAdmissions AIN102 Module 2
  • 28.
    Having Restricts the groups returned Operates on the groups after they have been formed Admissions AIN102 Module 2 HAVING (((Admissions.Diag_Code) Like "a*"))
  • 29.
    Having – SQLView Restricts the groups returned Operates on the groups after they have been formed Admissions AIN102 Module 2
  • 30.
  • 31.
  • 32.
    AIN102 Access QueryDesign Part 2 – Joins, Subqueries Admissions AIN102 Module 2
  • 33.
    Admissions AIN102 Module 2 Database Design Diagram of the database for reference in joins
  • 34.
    Joins Used tocombine 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 Admissions AIN102 Module 2
  • 35.
    Inner Join Pairseach 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 2
  • 36.
    Inner Join SyntaxAdmissions AIN102 Module 2 Basic SQL 92 Syntax: SELECT column-list FROM table1 [ AS alias ] INNER JOIN table2 [ AS alias ] ON join-condition
  • 37.
    Table Aliases Shorthandname for a table Used in more complex queries Admissions AIN102 Module 2 Select t.id, s.lname From _traveler as t Inner join xLU_Staff as s On t.staffid = s.staffid Table alias
  • 38.
    Inner Join BasicSQL Example Admissions AIN102 Module 2 Basic Example: Add patient names to admissions data Two join tables Join condition
  • 39.
    Inner Join QueryDesign Admissions AIN102 Module 2
  • 40.
    Inner Join ResultsAdmissions AIN102 Module 2
  • 41.
    Inner Join overMultiple columns Note that that the join condition can apply to multiple columns if desired Used with composite keys Admissions AIN102 Module 2 Select …. From tablea as ta Inner join tableb as tb On ta.key1 = tb.key1 And ta.key2 = tb.key2
  • 42.
    Joining More thanTwo Tables Can join several tables in one select Try to limit to three or four Demonstration example Admissions AIN102 Module 2
  • 43.
    3 Table QueryResults Admissions AIN102 Module 2
  • 44.
    More on AliasesCan be set in Query Designer, too, by right clicking on a table Useful for debugging top level queries as we shall see later Admissions AIN102 Module 2
  • 45.
    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 2
  • 46.
    Left Outer JoinAdmissions AIN102 Module 2 Basic SQL 92 Syntax: SELECT column-list FROM table1 LEFT JOIN table2 ON join-condition
  • 47.
    Left Outer Join(Right click on relationship to edit) Admissions AIN102 Module 2
  • 48.
    Left Outer JoinModify 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? Admissions AIN102 Module 2
  • 49.
    Left Outer JoinResults Admissions AIN102 Module 2
  • 50.
    Subqueries One selectstatement embedded in another Can be nested multiple levels deep In Access query designer can be placed in criteria row or field row Admissions AIN102 Module 2
  • 51.
    Multi-valued Subquery Atype of subquery that compares to a list Ex: find all diagnostic codes with no admissions Commonly encountered in commercial practice Admissions AIN102 Module 2
  • 52.
    Multi-valued Subquery Placesubquery in criteria row Admissions AIN102 Module 2
  • 53.
    Multi-valued Subquery ResultAdmissions AIN102 Module 2 No one was ever admitted with these diagnoses.
  • 54.
    Single-valued Subquery AdmissionsAIN102 Module 2 Subquery that returns a single value Find all admissions with stays greater than the average stay
  • 55.
    Single-valued Subquery AdmissionsAIN102 Module 2 Subquery that returns a single value
  • 56.
    Single-valued Subquery ResultAdmissions AIN102 Module 2
  • 57.
    Queries using QueriesQueries 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 2
  • 58.
    Queries using QueriesAdmissions AIN102 Module 2 Filter the prior query ( a little on the complicated side) for the year 2001
  • 59.
    Queries using QueriesResult Admissions AIN102 Module 2
  • 60.
    AIN102 Access QueryDesign Part 3 – Unions, Action Queries Admissions AIN102 Module 2 D. H. D’Urso and Associates 949-489-1472 http://www.dhdursoassociates.com
  • 61.
    Special Queries Mostare “Action Queries” Reached through Query pull-down menu Admissions AIN102 Module 2
  • 62.
    Special Queries Specialaction queries have distinct symbols Admissions AIN102 Module 2
  • 63.
    Admissions Database AdmissionsAIN102 Module 2 Diagram of the database for reference in part 3
  • 64.
    Data Modification QueriesAdmissions AIN102 Module 2 SQL Action Query INSERT Append UPDATE Update DELETE Delete SELECT…INTO Maketable
  • 65.
    Creating Action QueriesFirst 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 2
  • 66.
    Select Into…Maketable Createsa new table “on the fly” Admissions AIN102 Module 2
  • 67.
    Select Into…Maketable AdmissionsAIN102 Module 2 New table Existing table Make Table
  • 68.
    Archived Labor History Admissions AIN102 Module 2
  • 69.
    Delete SQL Deletesone or more rows Admissions AIN102 Module 2 Basic Syntax: DELETE FROM table-name WHERE filter-criteria
  • 70.
    Delete Admissions AIN102 Module 2 Example: Delete all archived records newer than 12/31/2001
  • 71.
    Delete SQL AdmissionsAIN102 Module 2 Example: Delete all archived admissions_history newer than 12/31/01
  • 72.
    Archive after DELETEAdmissions AIN102 Module 2
  • 73.
    More complex DELETEwith 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 2
  • 74.
    Append Query (SQLInsert) Adds new rows to an existing table Two forms: Single Row Multi-Row Admissions AIN102 Module 2
  • 75.
    Single Row InsertAdmissions AIN102 Module 2 Basic Syntax: Insert [ into ] table-name Values ( value-list )
  • 76.
    Single Row InsertAdmissions AIN102 Module 2 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.
  • 77.
    Append Query (Multi-rowinsert) Admissions AIN102 Module 2
  • 78.
    Multi-row Insert SQLAdmissions AIN102 Module 2 Basic Syntax: INSERT [ INTO ] table-name SELECT select-statement
  • 79.
    Multi-row Insert (Append)Admissions AIN102 Module 2 Add back archived admissions_history newer than 12/31/01
  • 80.
    Append (Insert) ResultsAdmissions AIN102 Module 2
  • 81.
    Update Updates fieldsin an existing row Admissions AIN102 Module 2 Basic Syntax: UPDATE table-name SET field1 = new value , field2 = new value ,… WHERE selection-criteria
  • 82.
    Update in QueryDesigner Admissions AIN102 Module 2 New value
  • 83.
    Update Increase theadmission_history copays by 10% ( unrealistic to change history but we don’t want to step on our good tables) Admissions AIN102 Module 2
  • 84.
    Update Results AdmissionsAIN102 Module 2 New values Remember: you have to look at the affected table. No “result” is displayed.
  • 85.
    Unions Combines theresults 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 2
  • 86.
    Union Syntax AdmissionsAIN102 Module 2 SELECT column-list FROM table1 UNION [ ALL ] SELECT same-columns FROM table2
  • 87.
    Union Example AdmissionsAIN102 Module 2
  • 88.
    Union Result AdmissionsAIN102 Module 2
  • 89.
    When you wantto know more ½ day workshops. 1 day SQL200A Microsoft Access SQL Class. Admissions AIN102 Module 2
  • 90.
    End Session 2Admissions AIN102 Module 2 End of Class!
  • 91.
  • 92.