Upload
phamtu
View
217
Download
0
Embed Size (px)
Citation preview
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
Introduction to Access97 - Session 4Selection Queries (1)
by Robin Beaumont
e-mail: [email protected]
Contents
1. LEARNING OUTCOMES CHECK LIST FOR THE SESSION........................................................................2
2. INTRODUCTION........................................................................................................................ 3
3. CREATING A QUERY................................................................................................................ 4
4. THE QUERY DESIGN WINDOW..............................................................................................5
5. ADDING FIELDS TO THE RESULT.........................................................................................6
6. RUNNING A QUERY.................................................................................................................. 8
7. SAVING A QUERY..................................................................................................................... 8
7.1 SAVING A QUERY DEFINITION...................................................................................................87.2 SAVING THE RESULTS OF A QUERY...........................................................................................8
8. CLEARING THE QBE GRID AND ADDING TABLES............................................................8
9. SPECIFYING A QUERY............................................................................................................. 8
10. SPECIFYING CRITERIA FOR NUMBER FIELDS..................................................................9
10.1 A SPECIFIC VALUE................................................................................................................... 910.2 A RANGE OF VALUES.............................................................................................................10
11. SPECIFYING CRITERIA FOR TEXT FIELDS......................................................................10
11.1 A SPECIFIC VALUE.................................................................................................................. 1011.2 A RANGE OF VALUES.............................................................................................................1111.3 WILD CARDS.......................................................................................................................... 12
12. SPECIFYING CRITERIA FOR DATE FIELDS......................................................................13
12.1 A SPECIFIC VALUE.................................................................................................................. 1312.2 A RANGE OF VALUES.............................................................................................................1312.3 A RANGE OF VALUES BEFORE OR AFTER TODAY......................................................................14
13. MULTIPLE CRITERIA.............................................................................................................14
13.1 AND...................................................................................................................................... 1413.2 OR........................................................................................................................................ 16
14. BLANK RECORDS - LOOKING FOR, OR EXCLUDING FROM A QUERY......................18
15. REVIEW EXERCISES.............................................................................................................. 19
16. SUMMARY................................................................................................................................ 20
17. CHECK WHAT YOU HAVE LEARNT....................................................................................20
18. ANSWERS.................................................................................................................................. 21
18.1 HOW TO IMPLEMENT AN EXCLUSIVE OR (XOR)........................................................................2118.2 ANSWERS TO SELECTED 'REVIEW EXERCISES'..........................................................................22
This handout is part of a course. For details please see Section 7.1 at: http://robinbt2.free-online.co.uk/virtualclassroom/contents.htm
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 1
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
1. Learning outcomes check list for the sessionEach of the practical sessions aims to provide you with a number of skills (the 'be able to's' below) along with the relevant information (the 'know what's' below). After you have completed the session you should come back to these points ticking off those with which you feel happy.
Learning outcome Tick box
Describe what an Access query is
Describe the components of an Access query
Be able to create a query using the query design window
Be able to add tables to the query design window
Be able to run a query
Be able to save a query definition
Know how to save the results of a query to a table
Be able to specify criteria for number fields for one specific value and a range of values
Be able to specify criteria for text fields
Be able to explain and use wild cards
Be able to use the 'And' operator
Be able to use the 'OR' operator
Know the difference between 'OR' and 'XOR'
Be able to find or exclude blank (NULL) fields
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 2
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
2. IntroductionThis session will begin to investigate queries. Queries are a method of examining or manipulating one or more tables to obtain useful information. For example, we might wish to examine the patient table in the consultations database for the patients that live in a particular city or who where registered before 1970. This session will both demonstrate, and get you to carry out, such queries.
A query consists of three components:
The tables to query
Query definition
Result
These can be thought of as the input, process and output. The input is simply one or more fields from one or more tables in a database. The process is interrogating the table, using a special facility called QBE, and the output is the result. A result can be either the creation of a temporary dataset, the create of a results table or some type of updating to the current table.
This session will look specifically at Select queries which result in a new temporary set of records being produced based upon some such criteria defined in the QBE facility.
This session uses the cons2 database which contains the following records in the Doctor table.
Exercise:
Open the cons2 database now.
Consultations database, Doctor table.
Doc id
doc first name
doc Surname
Gender Date reg Addrs title Addrs st name
Addres st no
city postcodeA
postcodeB
Phone No
1 john SMITH 2 01/04/67 The old surgery
12 station rd
Walkergate Newcastle
Ne23 4rr 0191 009282
2 steve Jarvis 2 01/05/70 Morpeth HC
High st Morpeth Mo2 1ts 0127 485739
3 Mary Goodall 1 27/11/67 Seeham HC
The harbour
Seaham Se1 4jp 0191 345867
4 Fiona black 1 30/01/76 Cragside HC
The East Wing
Cragside castle
Cr3 1ws 0146 389431
5 Anna Scriabin 0 25/03/61 Uniiverisity MC
Newcastle Univeristy
Newcastle
Ne1 1aa 0191 445776
23 mark Goodall 2 27/02/55 Seaham HC
The harbour
Seaham Se1 4jp 0191 345867
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 3
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
3. Creating a queryAt with most things in Access97 you can create them several different ways. Access97 provides query design Wizards, like the one you used earlier for creating a form, however we will develop the queries in this session using the query design window. Opening this window is left as an exercise for you.
Creating a query Exercise:
In the database window select the query tab then click the New button. The relevant dialogue boxes are pictured below.
From the 'New Query' dialogue box' select the 'Design view' option then click on the 'OK' button.
Query help Exercise:
Call up Access97 office assistant help by pressing the F1 key or the appropriate toobar icon. Read through the following help topics
'Queries what they are and how they work' (read all three pages)
'Create a Query' (just have a quick skim through the material. You are not expected to understand much of it at the present).
'Switch between views of a query' Then click on the 'design view', Datasheet view' and 'SQL view' each of the three views
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 4
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
4. The Query design WindowYou will now be presented with the query design window, this is one of the most complex in Access. And what makes it worse, is the fact that it is partially obscured by the Show table dialogue box, asking you which tables you would like to query in the present database.
In the following sections, we will carry out a number of queries on the 'doctor' table. As a preliminary step the table needs to be added to the query design window.
Exercise:
From the 'Show table' dialogue box Choose the doctor table then click the add button.
Click the close button on the dialogue to hide it for now. Note if you need it again you can easily call it up from the menu.
You should now have something that looks like the picture below.
The query window consists of two parts. The top part shows graphically what tables you are using to develop the query. The bottom part is a graphical query tool using a method known as QBE (Query By Example) this is also used in other databases so learning QBE in Access means you can transfer to another database relatively easily.
Rather than describing all the features of the query design window, of which there are many, we will carry out a few very simple examples using firstly the doctor and than the patient tables.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 5
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
5. Adding fields to the resultThe first thing to do is to decide for which fields you want to see the results. This is achieved by dragging the necessary fields from the top part of the window to the grid in the bottom part. You can drag individual fields by selecting each individually or, alternatively choosing the asterisk at the top of the field list and dragging that to choose all the fields. We will use yet another method.
Selecting all the fields form a table table for a query exercise:
The QBE grid should now look similar to the picture below.
Depending upon the way Access97 has been setup on your machine you may have an additional row labelled 'Table:' along with the value 'doctor' in each cell. This additional information is useful if you have a query that uses more than one table. For now it doesn't matter. You can hide or show the row by going to the menu option view -> table names if it worries you.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 6
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
Inspecting the grid you should note:
The field names are listed along the top.
The 'sort' cells are empty. You request various options by clicking on the cells.
A show button is clicked for each field. You may be wondering why you would need a field in the QBE window that allows you to include fields in the query that you do not intend to show. There are many reasons for this. You may wish to select records based upon certain criteria for a field which you do not wish to be displayed in the result. For example you may want to choose all doctors that were registered after a certain date without actually displaying the date in the result.
Query grid help. Exercise:
Call up Access97 office assistant help by pressing the F1 key or the appropriate toolbar icon. Read the following help topic 'Ways to customize a query'. The help screen is shown below.
Before we develop our first query the following sections describe a few of the tasks you need to be familiar with.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 7
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
6. Running a queryOnce you have specified a query you need to run it to obtain the result. You can either choose the menu option Query run or use the speed button with the exclamation mark (!) on it.
7. Saving a queryAs mentioned above a query is really a definition and a result. Therefore, Access offers you the opportunity to save either of these.
7.1 Saving a query definitionTo do this you either choose the menu option file save when you have the query window open or click on the save button on the speed bar. In the following exercises this is the way you are expected to save the queries.
7.2 Saving the results of a query
The information below is given for reference.This is slightly more complex. Unfortunately, you can not save the results once you have obtained them. What you need to do is specify the table you wish to results to form as part of the query definition. To do this:
In the query definition window:
Choose the menu option Query Make table. The dialogue box pictured below will then pop up. Enter the name of the table you wish the results to go to. Then click OK.
8. Clearing the QBE grid and adding tablesThere are two ways to do this:
Menu option Edit clear grid or Select the table(s) at the top part of the window then press delete.
Tip: To add a table to a query choose the menu option: Query add table
9. Specifying a queryThe most important aspect of queries is actually specifying what they should do. You will be specifying what they do using a special query language called QBE (Query By Example). This is often referred to as query building.
The next section will demonstrate how to build select type queries. These 'select' queries, allow you to select a sub set of records from one or more tables based on certain criteria which is hardly surprising given their name! The criteria you enter into the various cells are often called expressions or statements. The doctor table will be used for all the examples and initial exercises.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 8
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
10. Specifying criteria for number fields10.1A specific valueExample: To Select all the female doctors form the doctor table.
We would set the query grid up in the followng manner. Remember we gave the doctors the following coding for gender; 1=female;2=male. Therefore, all we need to do is place a 1 in the criteria box of the gender field in the QBE grid:
Field doc Surname Gender
Sort:
Show Criteria 1
Or:
Select query Exercise 1:
Set-up the query grid in the manner shown above, that is add '1' to the criteria cell of the gender column, and then run the query. You should end up with the following result:
Result:
Doc id doc first name
doc Surname
Gender Date reg Addrs title Addrs st name Addres st no
city post
codeA
post
codeB
Phone No
3 Mary Goodall 1 27/11/67 Seeham HC
The harbour Seaham Se1 4jp 0191 345867
4 Fiona black 1 30/01/76 Cragside HC
The East Wing Cragside castle
Cr3 1ws 0146 389431
5 Anna Scriabin 1 25/03/61 Uniiverisity MC
Newcastle Univeristy
Newcastle Ne1 1aa 0191 445776
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 9
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
10.2A range of valuesA large number of queries search for a range of values rather than a specific one. This is achieved by using various numeric operators such as (<,>, >=, <=, <>, between). For example:
Expression Meaning
>234 Select all records with a value greater than 234
<150 Select all records with a value less than 150
Between 1 AND 3
Select all records between 1 and 3 inclusive
<>1 Select all records not equal to 1
11. Specifying criteria for text fields11.1A specific valueExample: To select all the doctors with the surname goodall from the doctor table
Looking at the records in the Doctor table there are two with the surname goodall. To search for all the doctors with that name we enter goodall in the criteria box of the surname field in the QBE grid:
Field doc firstname Surname
Sort:
Show Criteria goodall
Or:
Select query Exercise 2:
Set-up the query grid in the manner shown above, that is add 'goodall' to the criteria cell of the 'surname' column. Then run the query. Remember to clear any other criteria you may have specified from previous queries.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 10
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
Result:
Doc id
doc first name
doc Surname
Gender
Date reg
Addrs title
Addrs st name
Addres st no
city postcodeA
postcodeB
Phone No
23 mark Goodall
2 27/02/55
Seaham HC
The harbour
Seaham
Se1 4jp 0191 345867
3 Mary Goodall
1 27/11/67
Seeham HC
The harbour
Seaham
Se1 4jp 0191 345867
Although the word goodall was entered in the criteria box without quote marks around it Access97 adds them when it runs the query. Access is actually very forgiving when you’re not quite sure how exactly to type in the query, for example all these expressions would work:
Goodall
=goodall
"goodall"
="Goodall"
Another thing to note is that, depending upon how your computer has been set-up, Access may not be case sensitive. This means that if you run a query searching for GOODALL and then another one searching for GooDAll you may end up with the same result.
11.2A range of valuesAs with number you can search strings using various numeric operators such as (<,>, >=, <=, <>, between) can be used. For example:
Expression Meaning
>beaumont Select all records from beaumont to the end of the alphabet
<Xandua Select all records from Xandua to the beginning of the alphabet
Between black And jarvis
Select all records in the alphabet between black and jarvis inclusive
<>Beaumont Select all records not equal to Beaumont
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 11
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
Example: To select all the doctors records with a surname between black and jarvis from the doctor table would require the QBE grid to be set in the following manner:
Field doc firstname doc Surname
Sort:
Show Criteria Between black And jarvis
Or:
Select query Exercise 3:
Set-up the query grid in the manner shown above and run the query. Remember to clear any other criteria you may have specified from previous queries.
You should end up with the following result (the records may be in a different order):
doc first name doc Surname ….
mark Goodall …
steve Jarvis …
Mary Goodall …
Fiona black …
11.3Wild cardsA wild card is a special character you can use in an expression to search for certain patterns. A question mark (?) stands for a single character and the asterisk (*) stands for any number of characters in the same position as the asterisk.
As with the double quotation marks Access97 adds additional text to your expression when you use wild cards:
If you enter this expression
Access displays And finds these values
Sm?th Like "Sm?th" Smith, Smyth
L*ng Like "L*ng" Ling, Levering, Long may we keep singing
*th Like "*th" 128th, Perth
*on* Like "*on*" Condiments, Confections
*/10/93 Like "*/10/93" All dates in October 1993
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 12
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
12. Specifying criteria for date fields12.1A specific valueTo retrieve all records with a specific date you type in the date you are looking for.
12.2A range of valuesTo retrieve a range of date values you can use one of the expressions listed below.
Expression Meaning
01/01/* Finds all records for the first day of every year
*/02/78 All dates in Febuary1978
*/*/78 All records for 1978
See the section below if you wish to look for a period between two dates where either of them is the present date. To retrieve a range of dates you can use the between function i.e. Between 01/01/86 And 01/01/96. Notice that Access adds a # character to each side of the dates. It is also important to note that if you attempt to add dates with wild cards such as */12/78 you get an error message. The start and end date must be fully defined.
The following section concerning the dateadd function is for reference purposes only and can be ignored.
Reference only:
The dateadd function allows you the added flexibility of adding a particular time period - interval to a date. The method described above and in the next sub section works on days whereas this function takes into account leap years etc. When you use it you need to provide three pieces of information called parameters.
DateAdd(interval, number, date)
Parameter details:
interval this provides the interval of time you want to add to the date parameter. The Access help provides a complete list found by searching under dateadd. The most common ones are:
interval what to add to thefunction
Year yyyyMonth mWeek wwDay d
Number this is the number of intervals you want to add to the date. It can be positive (to get dates in the future) or negative (to get dates in the past).
Date This is the date you wish to add or subtract the interval from.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 13
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
12.3A range of values before or after todayTyping the expression Date() in the required field returns records for the current date. This expression can be adapted to search for ranges such as:
Expression Meaning
Between Date() And Date()-30 Records for the last thirty days
Between date() And Date() -365 Records for the last 365 days
Between date() And Date() +10 Records for the next ten days
13. Multiple CriteriaThe above example uses two words (between and and) to specify the query. Besides these words QBE recognises several others (e.g. null, xor, or) which allow the specification of very complex queries. We will now investigate two of the most important words QBE recognises And and Or.
13.1AndThis word is used when looking for multiple criteria which only returns records when all the criteria are found.
13.1.1 Several fields
Example: To retrieve all the records where the Doctor Surname=goodall AND gender = 1.
Field surname Gender
Sort:
Show Criteria Goodall 1
Or:
Select query Exercise 4:
Set-up the query grid in the manner shown above and run the query. Remember to clear any other criteria you may have from previous queries.
Result:
doc first name doc Surname Gender …
Mary Goodall 1 ……
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 14
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
13.1.2 One field
This situation can appear rather confusing. Consider the criteria "goodall And black" for the doctor surname field. At first it might appear that three records should be retrieved. However, if the query is run no records are returned. This is because there are no surname fields in which both the word goodall and smith occur together. If the 'And 'had been replaced with an 'Or'the required result would have been obtained.
Example: If you want to obtain all records with a PostcodeA beginning with an 'n' and ending with a '1' would require the expression 'PostcodeA = *1 AND n*' in the criteria cell of the 'postcodeA' column.
Field PostcodeA …
Sort:
Show Criteria Like "*1" And Like "n*"
Or:
Select query Exercise 5:
Set-up the query grid in the manner shown above and run the query. Remember to clear any other criteria
Result:
Doc id doc first
name
doc Surname …. city postcodeA
postcodeB
Phone No
5 Anna Scriabin ….. Newcastle Ne1 1aa 0191 445776
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 15
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
13.2OrThis word is used when looking for multiple criteria which returns records when ONE OR more of the criteria are found. This is often called an inclusive OR because it includes records that would be chosen when all the expressions are true.
13.2.1 Several fields
Example: To select all the records with the doctors surname = goodall OR / AND are females requires the QBE grid to be setup in the following manner:
Field Doc surname Gender
Sort:
Show Criteria "goodall"
Or: 1
Note: You can add up to eight more 'or's criteria using this method. If you ever need to use more you can save the result and then continue once again or use the more advanced SQL language.
Select query Exercise 6:
Set-up the query grid in the manner shown above and run the query. Remember to clear any other criteria.
Result:
Doc id doc first name doc Surname Gender
23 mark Goodall 2
3 Mary Goodall 1 both criteria are true for this record
4 Fiona black 1
5 Anna Scriabin 1
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 16
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
13.2.2 One Field
Example: To find all those doctors with the surname goodall OR smith:
Field Doc surname …
Sort:
Show Criteria "goodall"
Or: "smith"
Note: alternatively, you can type on the same line smith OR goodall.
Select query Exercise 7:
Set-up the query grid in the manner shown above and run the query. Remember to clear any previous criteria you may have in the grid first.
Result:
Doc id doc first name doc Surname …
23 mark Goodall …1 john SMITH …3 Mary Goodall …
13.2.3 Xor
You can also use the Xor term for an exclusive 'OR', that is one that leaves out the records that evaluate true for all criteria. This requires you to type the complete expression in one cell. To do this you may need to reference a field in another column achieved by placing the field name in square brackets. You can think of the two different types of 'or' as two different Venn diagrams
In this course, you are not expected to be able to produce 'xor' expressions but only to be aware of the difference. In the real world it is particularly important to be aware of the ambiguity in the 'or' term when requesting data from other people.
Optional exercise for insomniacs: Consider how you might go about extracting records which are chosen if the first expression is true or alternatively the second expression but not if both were true (an exclusive OR). I.e. the previous example would have left out the Mary Goodall record. [the answer is at end of the handout ]
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 17
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
14. Blank records - looking for, or excluding from a queryThis is frequently done to check to see how reliable the data is. It is achieved by adding the word 'null' to the required field.
Example: To select all records in which the address street number field is empty we type in the criteria cell for the 'Addres st no' column the word 'null'.
Field Addres st no …
Sort:
Show Criteria null
Or:
Select query Exercise 8:
Set-up the query grid in the manner shown above and run the query. Remember to clear any previous criteria you may have in the grid first.
Result: One record for Anna Scriabin.
To exclude blank records add the logical opposite to null which is not null.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 18
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
15. Review exercises Besides a set of doctor records the con2 database contains a set of records in the patient table.
patient id
Title first name Surname Doc id
DOB Gender Date on list
No children
Addrs title
Addrs st name Addres st no
city postcodeA
postcodeB
1 mr john smith 23 19/02/45 2 01/02/85 3 Cherry bud
Orchard Lane 45 newcastle NE5 2pn
2 miss shella jones 23 02/01/55 1 01/02/70 Cathedral view
Cathedral Row 11 durham Du2 1b
3 prof richard farmer 23 06/12/55 2 01/02/89 2 Tosson tce 134 newcastle Ne2 3no
4 mr John Hewitt 1 23/01/71 2 01/02/96 1 Dovetail Gardens
9 Stanley St1 5th
5 miss shirley anderson 1 15/01/60 1 01/02/93 3 Newton Rd 23 newcastle NE2 3jl
Complete the following exercises using the Patient table, in the Cons2 database. You should check the results you obtain from the computer with the actual table given above. You can find the results in the final section of this handout.
1. List all males patients that are on the list
2. List all patients of doctor Smith (ID = 1)
3. List all patients that live in a house with a house number less than, but not including, 20. Note you may need to change the field type to a number of some variety if you get the wrong answer first?
4. List all patients with a first name of john
5. List all patients with a surname that begins with the letter h
6. List all patients that have a surname that begins with the letter h or prior to that in the alphabet
7. List all patients that have a surname that begins with the letter h to t, inclusive, in the alphabet
8. List all patients that have the first name John or Richard
9. List all patients that jointed the practice list in 1970
10.List all the patients that jointed the practice list between 1985 and 1995 inclusive
11.List all the patients that have more than two children (do not include those with only two) and also live in Newcastle
12.List all the patients for which there is no record of how many children they have
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 19
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
16. SummaryIn this session we have started to look at QBE, one of the methods Access provides for querying databases. We concentrated on one particular type of query, the select type and went through the process of creating, specifying and saving the definitions of such queries. Although this is only the very tip of querying databases, these very simple select queries can provide some very useful data. In the next session we will look at creating summary fields, additional derived fields and adjusting the layout of the results.
Expressions query help Exercise:
Call up Access97 office assistant help by pressing the F1 key or the appropriate toobar icon.:
Type in: 'Examples of criteria expressions for queries and filters' click the search button, then select the correct title (second one listed).
You are then presented with the first help screen which once again asks you 'what do you want to do?' select the following to topics:
'Learn how to enter expressions in queries and filters' (only look at a few pages).
'Learn how to create an expression' (only look at a few pages).
17. Check what you have learntNow go back to the beginning of the material for the session and read through the 'Learning outcomes check list' for the session. How many can you tick? If you are not sure about any of them work through the relevant practical exercies again. If you still have problems please contact me.
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 20
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
18. Answers18.1How to implement an exclusive or (xor)This is rather a tricky problem. There are many ways to solve it. Three are given below.
1. One of the easiest in QBE is to put the negatives in the diagonal cells.
(meaning find all the doctors with the surname goodall which are not females And find all the females which are not named Goodall.
Field Doc surname Gender
Sort:
Show Criteria "goodall" <>1
Or: <> goodall 1
2. Use the xor word in the cell and type in the complete expression, referencing other fields by enclosing the name in square brackets.
Field ([doctor].[doc surname] = "goodall") XOR ([doctor].[gender] = 1)
Sort:
Show Criteria <> false
Or:
3. Additional note for those who know SQL. It is interesting to note that if you approach the problem from a SQL perspective Access does something rather strange. The original SQL is:SELECT DISTINCTROW *
FROM doctor
WHERE ((doctor.[doc Surname]="goodall") OR (doctor.Gender=1));
Obviously the simplest solution in SQL is to just change the 'or' to 'xor' in the above expression. If you do this it works.
Alternatively In terms of SQL the logical step would be to add another statement which evaluated to false when both the doctors name = goodall and the gender = 1. Doing this in SQL results in the following:SELECT DISTINCTROW *
FROM doctor
WHERE ((doctor.[doc Surname]="goodall") OR (doctor.Gender=1)) and not ((doctor.[doc Surname]="goodall") and (doctor.Gender=1));
The resulting QBE is rather confusing:
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 21
Introduction to Health InformaticsAccess practical session 4 - Selection Queries (1)
18.2Answers to selected 'Review exercises' 1 Gender = 2 Answer is three patients
2 Left for you
3 Address st. no < 20 [If you changed the field type to number]
4 Left for you
5 Surname = h*
6 Surname <= h*
7 Surname BETWEEN h* AND t*. Answer has four patients
8 First name = john or richard. Note these can be entered in one cell or on two lines. Answer has three records.
9 On list = */*/1970. Answer has one record.
10 On list BETWEEN 01/01/85 AND 30/12/95. Answer produces three records.
11 Children > 2 AND city = newcastle. Answer produces two records.
12 Left for you to find out!
Document info:
Robin Beaumont Tel:0191 2731150 e-mail: [email protected]
Source: Laptop; C:\HIcourseweb new\chap8\s4\access4.doc
Date: 04/04/2000 09:51
Robin Beaumont 09/05/2023 Tel:0191 2731150 e-mail: [email protected] Source: Laptop; /tt/file_convert/5b357fc77f8b9a330e8d41fd/document.doc Page 22