Creating a Query

advertisement
Access 2007
Querying a Database
Sheila Dixon & Diane Skinner – September 2010
Creating a Query
Adding Criteria
A query is used to extract specific information from
tables. You can choose the fields and records to be
displayed and the order they are to appear in.
Adding criteria to the query allows you to determine
which records will be displayed. The comparison value
can be text, number or dates
1.
1.
Click on the Query Design button on the
Create ribbon.
2. Choose the table(s) to
be queried from the list
displayed.
2. Enter the criteria to be matched.
3. Click on the Run Button.
3. Click on Add.
Selecting a Range of Records
Using comparison operators
4. Click on Close.
1.
Adding Fields to the QBE Grid
1.
QBE Grid
In the example below the Year Joined field is being
searched.
Query
Unless you state otherwise, the records will be
displayed in primary key order.
1.
Click in the criteria row below the field you wish to
compare.
2. Enter one of the
comparison
operators
>, <, >=, <=
followed by the
figure to be
compared.
Field List
Double click on the
field name in the list
to copy it to the grid.
Sorting Records in a
Click in the criteria row below the field you wish to
compare.
Click in the Sort row below the field to be sorted.
2. Click on the drop-down list at the side of the sort
row and select Ascending.
Criteria
Expression
Displays records of students
who graduate
2010
in the year 2010
>2010
after 2010
<2010
before 2010
>=2010
in the year 2010 or later
<=2010
in the year 2010 or before
<>2010
any year except 2010
NOT can be typed instead of <>
ICT Learning
1
v_A07_03/1
Using the Between Operator
Saving a Query
This operator is used to find records within a given
range, but the upper and lower limits are inclusive i.e.
in the example below the 1/1/2011 and the 31/1/2011
would be included.
1.
1.
2. Type the query name.
3. Click on OK.
Add the field to the grid.
Multiple Criteria
2. In the criteria type BETWEEN, press the space bar,
then enter the lower limit.
Very often you want to extract data that matches more
than one criterion, this can be achieved using the AND
and OR logical operators. The criteria to be matched
are listed in rows, where each criteria row is a set of
conditions. AND and OR are known as Logical
Operators.
Creating Queries using OR
3. Press the space bar again, type AND, leave a space,
then enter the upper limit.
1.
Using the Like operator it is possible to find characters
that are part of a field, either at the beginning, the end
or somewhere in the middle. This is achieved using
wild cards.
Any single character e.g. sm?th would return
Smith and Smyth.
*
Zero or more characters e.g.
Henry* - all beginning with Henry.
*Henry - all ending with Henry.
*Henry* - containing Henry.
3.
This can be repeated for multiple ORs.
Another way of applying the OR condition is to add it
as a separate row, where either one row OR another
must be matched. (You can have up to 8 OR rows.)
When the * wild card is used it does not necessarily
return only whole words, for example searching for
*and* would return Hand, Land, Sandwich etc.
1.
Add the field to the grid.
2. In the criteria type in the first value press the
spacebar and type OR press the spacebar, then
enter the alternative value.
Partial Matches (Like Operator)
?
Clink the Save button on the Quick Access toolbar.
In the criteria row of the field type the part of the
field you know with appropriately placed wild card
operators. (The example below searches for those
with a surname beginning with the letter s).
Creating Queries using AND
1.
If you press the enter key the criteria will appear as
Like “s*”
Add the field to the grid.
2. In the criteria type in the first value press the
spacebar and type AND press the spacebar, then
enter the alternative value.
The Like operator is added automatically, as are the
speech marks.
In Operator
2. Click on the Run button.
The In operator is useful for finding an exact match
from a pre-defined list e.g.
In(Hull, Beverley, Leeds)
is the same as typing Hull OR Beverley OR Leeds.
ICT Learning
2
v_A07_03/1
Creating a Query that asks for Criteria
(Parameter Query)
1.
Totalling Queries
Creating a Totalling Query Using a Wizard
Create a new query in Design View.
1.
2. Add the table required to the QBE Grid.
Click on the Query Wizard button on the Create
ribbon.
3. Close the Add table dialog box.
2. Choose the Simple Query Wizard and click on OK.
4. Add the fields to the QBE grid as normal.
3. Select the Table/Query to base the query on from
the list.
5.
Position the cursor in the criteria row below the
field required.
4. Highlight the fields required in the query and add
them to the selected fields list by clicking on the
single headed arrow button.
6. Add the message you want to appear in the
parameter
dialog box,
enclosing it
in square
brackets.
5.
6. Choose to create a Summary query and click on the
Summary options button.
7.
7.
When you open or run
the query, the
parameter box will
appear.
Tick the summary totals required, click on OK,
then Click on Next.
8. Type a title for the query and click on Finish.
Creating a Totalling Query Manually
1.
8. Enter the criteria required.
Using Wild Cards in Parameter Queries
1.
When all the required fields have been added, click
on Next.
Create a new query in design view and add the
table required.
2. To add the Totals row click on the Totals
button on the Design ribbon.
As steps 1 to 8 above.
2. Click in front of the opening square bracket and
add the word Like.
3. You can then choose the field to group by and the
fields to perform summary calculations on.
Adding Criteria to Calculated Totals
To add criteria to a field already included in the query,
simply type in the criteria row.
Calculating Totals
3. When you run the query add the wild cards
characters as required.
Creating a Parameter Query to display partial
matches
Calculations on groups of records can be performed
using total queries (these are sometimes known as
aggregate queries). The types of calculations that can
be performed are known as functions.
1.
Function
What it does to the field
Sum
Totals the values
Avg
Calculates the mean average of values
Max
Displays the maximum value
Min
Displays the minimum value
Count
Counts the number of values in the field
(excludes those that are blank)
StDev
Returns the standard deviation
Var
The variance of values in a field
First
The first value
Last
The last value
As steps 1 to 9 in the instructions for creating a
parameter query.
2. Click in front of the opening square bracket and
type Like * &
3. Click after the closing square bracket and type & *.
4. When you run the query, the parameter box will
appear.
5.
Enter the criteria required.
The Where and Expression options on the Total dropdown list box are for more advanced calculations.
6. Any records where the field contains the text
entered in the parameter box will be displayed.
ICT Learning
3
v_A07_03/1
Querying Multiple Tables
The dialog box below is displayed
Queries can be used to bring together related data from
multiple tables.
1.
Create a new query in Design View.
2. Add the tables required to the grid.
The relationships defined between the tables
appears automatically.
2. Select the join type required and click on OK.
Adding the Field List
3. Close the Add table dialog box.
If you forget to add the field list it can be displayed at
any time:
4. Add the fields to the grid as normal.
1.
5.
To see the results of your query click on the
Datasheet view.
Adding All the Fields to the QBE Grid
The number of records displayed is determined by the
number of records in the “Many” side of the
relationship, so the data from the “One” table may be
duplicated in the resulting datasheet. However, a
record from the “One” table will only appear if there is
a corresponding entry in the many table. So for
instance if a student hasn’t registered for any modules
there record will not be displayed.
1.
1.
3. Press the delete key on the keyboard.
Hiding Fields in QBE Grid
1.
Untick the Show box in the column containing the
field you want to hide.
Deleting a Criteria Row
Also known as an
“equi-join”. Only
displays when there
is a record in both
tables.
1.
Position the mouse pointer to the left of the row to
be deleted.
2. Click once to select the row.
3. Press the delete key on the keyboard.
Displays if a record is
present in the table
from which the arrow
points.
Renaming a Query
If a mistake is made in naming the query (or any other
Access object), it can be rectified in the Navigation
pane:
To change the join type of a relationship in a
query:
1.
Position the mouse pointer over the top of the
column containing the field to be deleted until it
changes to a downward pointing arrow.
2. Click once to highlight the field.
The default join type displays a row only when there is
a record in the two related tables. There are two join
types:
Outer join
Double click on the *(asterisk) in the field list.
Removing Fields from the QBE Grid
Changing Join Types
Inner join
In query design view click on the Show Table
button on the Design ribbon.
1.
Right click on the query to be renamed.
2. Choose Rename from the shortcut menu that
appears.
Double click over the join.
3. Type the new name.
Deleting a Query
1.
Click on the Query tab in the navigation pane.
2. Highlight the query to be deleted.
3. Press the delete key on the keyboard.
4. Click OK to the message that appears confirming
deletion.
ICT Learning
4
v_A07_03/1
Download