2.8 Creating a Query
-
Upload
jamie-carter -
Category
Documents
-
view
216 -
download
0
Transcript of 2.8 Creating a Query
-
8/8/2019 2.8 Creating a Query
1/9
Access 2007 Quick guide to queries
1
Contents
Creating a Query ................................ ................................ ................................ ................................ 2
Using Wildcards in a query ................................ ................................ ................................ ................. 2Sum ................................ ................................ ................................ ................................ ................... 3
Count ................................ ................................ ................................ ................................ ................ 3
Not and Null Queries ................................ ................................ ................................ ......................... 4
Using Not ................................ ................................ ................................ ................................ ....... 4
Using Null ................................ ................................ ................................ ................................ ...... 4
Group By ................................ ................................ ................................ ................................ ........... 4
Example query using Group By, Count and Sum ................................ ................................ ................. 5
Creating the Query ................................ ................................ ................................ ........................ 5Using Mathematical Operators ................................ ................................ ................................ .......... 6
Access operators and their meaning ................................ ................................ .............................. 6
Average Values ................................ ................................ ................................ ................................ .. 7
Maximum and Minimum Values ................................ ................................ ................................ ........ 7
Calculated Fields in Queries ................................ ................................ ................................ ............... 7
Parameter Queries ................................ ................................ ................................ ............................ 8
Using wildcard selection................................ ................................ ................................ ............. 9
Using range selections ................................ ................................ ................................ ............... 9
-
8/8/2019 2.8 Creating a Query
2/9
Access 2007 Quick guide to queries
2
Creating a Query
You can create a query two ways, Click on the Create Tab to display the ribbon as above.
1. Query Wizard display a New Query popup which willstep you through the creation of a new query.
2. Query Design which takes you to the Query DesignScreen.
Using Wildcards in a query
In a query, it is often useful to select records without having an
exact match on a field. For example to select Sales peoplefrom their job titles, which may be sales assistant, sales supervisor, etc. This can be done using a
pattern search, which is simply a text search based on wildcard characters;
1. ? One unknown character2. * - Any number of unknown characters
Within a database
1. Create a new query in the Design View2. Place the required fields into the query grid3. For a field you need to search using the Wildcard
a. Click on the Criteria Rowb. Type *sales*. This is enough of the word to find all of the required occurrences
because * denotes any amount of text may occur before or/and after.
c. Access will change the criteria to Like *sales*.4. Now run the query.
-
8/8/2019 2.8 Creating a Query
3/9
Access uick gui e t queries
3
Sum
V
ss
yc
c
sc
bec
e out on nume
c
data within Access The most
common incudes finding the total and ave
age of a set of figures the maximum and minimum value
and counting the number of records
Thesecalculations arecarried out on a total line within a query
on the data.
nstead of dis
laying all records which met thecriteria match, ina sum query thecalculations are applied to theselected records and only the
summary result is displayed in the query.
1. Create a new query in the design view2. Clic on the Totals button. A new row will appear on the gridcalled
Total: with the default entry of Group By.
3. Clic in the Total: box for the field you which to total, then clic the down arrow to displaythe drop down list. Select Sum from this list.
4. Run the query.5. The answer tableshows only one field, SumOffieldname. This is the total value of all the
data.
Note:The Total row is removed by clicking again.
Cou
Thecount calculation is another summarycalculation, which counts how manyentries in a query
have thevalue in a particular field, including duplicatevalues. t does not count null entries, .E.
empty fields.
1. Edit above query in Design View. (Ensure Total: row is displayed)2. Clic
on the down arrow in the Total: box and select count.
3. Run the query, the answer tableshould giveyou a count of the number of items in the tablethat match your criteria.
Note:Take care when choosing which field to use in the count. If any records on the table did
not have an entry in the chosen field, they would not be included in the count.
-
8/8/2019 2.8 Creating a Query
4/9
Access 2007 Quick gui e t queries
4
Notand Null Qu ri
Queries may be applied to tables to excludecertain records when the query is run and therefore to
display all the records which do not match thecriteria.!t is also possible to search for fields that
have no data by using a Null query. Thiscan be useful when you want to makesure that all records
have been fullycompleted.
Using Not
1. Open your query in Design View2. " n thecriteria for your chosen field enter Not and thevalueyou wish to exclude. See the
example below;
3. Run the query and bikes will beshown apart from Giant.Using Null
1. Open your Query in Design View2. To find records which have no data in a selected field. Type Null in theCriteria box for the
required field.
3. Run the query and the items returned are the ones with data in your selected field.
Group By
Using a summarycode of Group By in a query does not show all selected records but will show a
result row for each different value in a field. Group By is often combined with other summarycodes
to producesub totals.
1. Open your query in deign view2. Display the Total: row byclicking the Totals button .3. Select Group By from the pull down menu.4. Now run the query
-
8/8/2019 2.8 Creating a Query
5/9
Access 2007 Quick guide to queries
5
Exa # ple query using Group By, Count and Su # The Example uses the Bike Sales data which has been imported into Access 2007 from a CSV fileon
the Edexcel website.
This query is going to show how many bikes of each model sold, using count, and the total amount
each model sold, using Sum.
Creating the Query
1. Click on the Create Tab on the Access Ribbon2. Use the Query Design Button3. From the table popup select the tables you need to select the data from.4. For this query; double click on the Make, Model, Model (Once for the data to appear and
the second so we and count) and Price
5. Click on the Totals button to display the Total: row.6. Click on the Total: row for Make and Select Group by from the pull down menu. Then do the
same for the first Model field in the grid.
7.
Click on the Total: row for the second Make field and select Count from the menu.8. Click on the Total: row for the Price field and select Sum from the menu.
-
8/8/2019 2.8 Creating a Query
6/9
Access 2007 Quick guide to queries
6
Using Mathe $ atical OperatorsWhere values are used in tables, queries can be performed which select records based on a range of
values, e.g. quantities more that a certain amount, prices less than a certain value, or ages within an
age range. To do this requires the use of a range operators such as < (Less than) in the criteria.
1. Open a query in Design View.2. Position the cursor in the criteria field for a numeric field of your choice and type, for
example ;
a. >500 will display all records with a value of greater than 500.b.
-
8/8/2019 2.8 Creating a Query
7/9
Access 2007 Quick guide to queries
7
Average ValuesAvg is another summary calculation. It calculates the average of all values in a field. It is selected
from the Total: row in the query grid, in a similar way as Count and Sum. All such summary
calculations can be combined with selection criteria to obtain statistics for certain parts of the data.
Maxi 1 u 1 and Mini 1 u 1 Values
Max and Min are summary calculations that will show the Maximum and minimum values of
specified fields for all selected records. A range of the highest and lowest values in a table can be
displayed using the TopValues property.
Calculated Fields in Queries
When a calculation is required in a query, based on the contents of one or more of the available
fields, a calculated field may be used. This is a field that the user creates, to display the results of a
calculation defined with an expression. The expression may involve one or more numerical fields,and involved and mathematical calculations. It allows, for example, two fields to be multiplied
together, or for a price field to be multiplied by a percentage, or discount applied to all values in a
particular field.
The name of the new field is entered in the Field row of a query grid, followed by a colon, :. The
expression is then entered, with field names enclosed in the square brackets.
Once a calculated field is defined, it can be used like any other field. It can be used for selection, for
sorting or appear in forms and reports.
The standard mathematical symbols are Add +, subtract -, Divide / and multiply *.
1. Ensure you are in the Design View of a query.2. Select the field your wish to include in your query.3. In an empty column enter an expression in to the Field: box.
For example Total Amount:[Price]*[Number Sold]
4. Total amount becomes a new field name, Price and Number Sold are the fields involved inthe calculation, and * is the mathematical operator. This is a new calculated Field that will
multiply the price of each product by the number of each product sold, and call the answer
Total Amount.
5. Run the Query
-
8/8/2019 2.8 Creating a Query
8/9
Access 2007 Quick gui e t queries
8
Param 2 ter Queries
3 f a query is being run frequently and all that ischanging is theselection criteria for one or two fields,
then a Parameter Querycan be used. A parameter Query displays a dialog box prompting for the
criteria information. Once the information isentered, the query is performed, with theentered
value being used as theselection criteria. The next time the query is run, a different valuecan beentered and used, without having to change the query design.
Parameterscan be used together with other types of query, such as partial queries using wildcards
and range queries.
A querycan contain as many parameter values as required. Simplyset a parameter value for each
required field. Every time the query is run, a dialog box will appear for each field that contains a
parameter value. The dialog boxes will appear in the order of the field names in the query grid, from
left to right.
1. Ensureyou are in the design view of a query.2. Add the fieldsyou require to the query grid.3. To create the parameter query in one of the fields, enter a square bracket [ followed by
instructions to the user which instructs them to what they need to enter, followed by a
closing square bracket ].
For Example: [Enter Town Required]
Note: Ensure that the prompt text between thesquare brackets is not thesame as any field name.
This will confuse Access.
4. Run the Query.A dialog box appears. Notice the text entered
between the[] in thecriteria box appears as the
prompt text. Thevalueentered in theentry box
will be used as thecriteria for Town.
5. Enter the Town you wish to find and click ok. Thequery will run showing all records where town equals what you entered into the prompt
box.
-
8/8/2019 2.8 Creating a Query
9/9
Access 2007 Quick gui e t queries
9
Using wildcard selection
Parameterscan be used with wild card selections.
1. Return to the query design view.2. 4 n thecriteria box for the a field you can enter;
Like[Enter Letter Required]&*
3. Run the query. When the dialog box appears, enter aletter and click ok.
When the query runs it replaces the[Enter Letter Required] with thevalueentered in the
prompt box as thecriteria.
4. The query will show all records where the field starts with the lettered entered.
Using range selections
Parameterscan be used with rangeselections. The user is able to select records between values.
1. Return to the query design view2. 5 n thecriteria box for your field you can enter;
>=[From Lower Value] and