Your SlideShare is downloading. ×
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
AIN102.2 Microsoft Access Queries
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

AIN102.2 Microsoft Access Queries

508

Published on

AIN102 Microsoft Access Queries Module 2. covers date and string functions, joins, subqueries, action queries, unions.

AIN102 Microsoft Access Queries Module 2. covers date and string functions, joins, subqueries, action queries, unions.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 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
  • 2. 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.
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. String Manipulation
    • Trim
    • Substring
    • UCase, LCase
    • Left, Right
    • See help for others
    Admissions AIN102 Module 2
  • 7. String Manipulation Admissions AIN102 Module 2
  • 8. String Manipulation Admissions AIN102 Module 2
  • 9. String Example Admissions AIN102 Module 2
  • 10. Date Functions
    • Numerous date functions
      • DatePart
      • DateDiff
      • DateAdd
      • Etc.
    • Often used:
      • Year
      • Month
    • Ex: where year(birthdate) = 1999
    Admissions AIN102 Module 2
  • 11. DateDiff
    • How long did patients stay in weeks?
    Admissions AIN102 Module 2
  • 12. DateDiff results Admissions AIN102 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 Month Function Admissions AIN102 Module 2
  • 16. 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
  • 17. Summary Functions
    • Count
    • Sum
    • Min
    • Max
    • Avg
    • Often used in conjunction with grouping
    Admissions AIN102 Module 2
  • 18. Summary Functions in Access 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 Summaries Admissions AIN102 Module 2 This query counts patient admissions In the year 2001
  • 21. Simple Column Summaries Admissions AIN102 Module 2
  • 22. Simple Record Count Admissions 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 results into summary rows, one per group
    • Groups can have sub groups which have sub groups and so on….
    Admissions AIN102 Module 2
  • 25. GROUP BY in Query Designer Admissions AIN102 Module 2
  • 26. 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!
  • 27. Group By Results Admissions 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 – SQL View
    • Restricts the groups returned
    • Operates on the groups after they have been formed
    Admissions AIN102 Module 2
  • 30. HAVING Admissions AIN102 Module 2
  • 31. HAVING Results Admissions AIN102 Module 2
  • 32. AIN102 Access Query Design 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 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. Inner Join Basic SQL Example Admissions AIN102 Module 2 Basic Example: Add patient names to admissions data Two join tables Join condition
  • 39. Inner Join Query Design Admissions AIN102 Module 2
  • 40. Inner Join Results Admissions AIN102 Module 2
  • 41. 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
  • 42. 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
  • 43. 3 Table Query Results Admissions AIN102 Module 2
  • 44. 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
  • 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 Join Admissions 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 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
  • 49. Left Outer Join Results Admissions AIN102 Module 2
  • 50. 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
  • 51. 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
  • 52. Multi-valued Subquery
    • Place subquery in criteria row
    Admissions AIN102 Module 2
  • 53. Multi-valued Subquery Result Admissions AIN102 Module 2 No one was ever admitted with these diagnoses.
  • 54. Single-valued Subquery Admissions AIN102 Module 2 Subquery that returns a single value Find all admissions with stays greater than the average stay
  • 55. Single-valued Subquery Admissions AIN102 Module 2 Subquery that returns a single value
  • 56. Single-valued Subquery Result Admissions AIN102 Module 2
  • 57. 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
  • 58. Queries using Queries Admissions AIN102 Module 2 Filter the prior query ( a little on the complicated side) for the year 2001
  • 59. Queries using Queries Result Admissions AIN102 Module 2
  • 60. 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
  • 61. Special Queries
    • Most are “Action Queries”
    • Reached through Query pull-down menu
    Admissions AIN102 Module 2
  • 62. Special Queries
    • Special action queries have distinct symbols
    Admissions AIN102 Module 2
  • 63. Admissions Database Admissions AIN102 Module 2 Diagram of the database for reference in part 3
  • 64. Data Modification Queries Admissions AIN102 Module 2 SQL Action Query INSERT Append UPDATE Update DELETE Delete SELECT…INTO Maketable
  • 65. 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
  • 66. Select Into…Maketable
    • Creates a new table “on the fly”
    Admissions AIN102 Module 2
  • 67. Select Into…Maketable Admissions AIN102 Module 2 New table Existing table Make Table
  • 68. Archived Labor History Admissions AIN102 Module 2
  • 69. Delete SQL
    • Deletes one 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 Admissions AIN102 Module 2 Example: Delete all archived admissions_history newer than 12/31/01
  • 72. Archive after DELETE Admissions AIN102 Module 2
  • 73. 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
  • 74. Append Query (SQL Insert)
    • Adds new rows to an existing table
    • Two forms:
      • Single Row
      • Multi-Row
    Admissions AIN102 Module 2
  • 75. Single Row Insert Admissions AIN102 Module 2 Basic Syntax: Insert [ into ] table-name Values ( value-list )
  • 76. 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.
  • 77. Append Query (Multi-row insert) Admissions AIN102 Module 2
  • 78. Multi-row Insert SQL Admissions 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) Results Admissions AIN102 Module 2
  • 81. 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
  • 82. Update in Query Designer Admissions AIN102 Module 2 New value
  • 83. 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
  • 84. Update Results Admissions AIN102 Module 2 New values Remember: you have to look at the affected table. No “result” is displayed.
  • 85. 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
  • 86. Union Syntax Admissions AIN102 Module 2 SELECT column-list FROM table1 UNION [ ALL ] SELECT same-columns FROM table2
  • 87. Union Example Admissions AIN102 Module 2
  • 88. Union Result Admissions AIN102 Module 2
  • 89. When you want to know more
    • ½ day workshops.
    • 1 day SQL200A Microsoft Access SQL Class.
    Admissions AIN102 Module 2
  • 90. End Session 2 Admissions AIN102 Module 2 End of Class!
  • 91. Notes Admissions AIN102 Module 2
  • 92. Notes Admissions AIN102 Module 2

×