Group/Summary Operations Change Join Properties Not Equal Query Parameter Queries Working with Text IIF Queries Expression Builder Backing up Tables Action Queries (Update, Make Table, Delete, & Append) Crosstab Queries Microsoft Access XP (2002) - Advanced Queries
32
Embed
Microsoft Access XP (2002) - Advanced Queriesinfo.marshall.usc.edu/.../Applications/Access/Access_2002-Adv_Quer… · Advanced Queries . USC - Marshall School of Business AIS ...
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Group/Summary Operations
Change Join Properties
Not Equal Query
Parameter Queries
Working with Text
IIF Queries
Expression Builder
Backing up Tables
Action Queries
(Update, Make Table, Delete, & Append)
Crosstab Queries
Microsoft Access XP (2002) -
Advanced Queries
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 2 of 32
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 18 of 32
Using ‘OR’ in IIF Statements
When the “Or” operator is used in IIF statements, data need only meet one of the possible conditions to
validate the true portion of the IIF statement.
Example:
Sweets Candies does business in three countries, USA, Canada, and the UK. They would like a list
that states whether the country is foreign or domestic.
1. From the Database window, click on the Tables tab.
2. Select the table: “Sweets Customers”
3. From the menu, click on: INSERT - QUERY
4. Select “Design View” and click on “Ok” .
5. Construct the query as shown.
6. Run the query .
Using ‘AND’ in IIF Statements
When the “And” operator is used in IIF statements, data must meet all of the possible conditions to
validate the true portion of the IIF statement.
In this example, if a customer is from the USA and their credit limit is over $30,000, they are
considered a “Preferred” customer, else they are a “Normal” customer.
Status: IIf([Country]="Canada" Or [Country]="UK","Foreign","Domestic")
Condition using the OR
operator. i.e. If country equals
Canada or UK…
What to do if
condition is
true.
What to do if
condition is
False.
Status: IIf([Country]="USA" And [Credit Limit]>30000,"Preferred","Normal")
Condition using AND True False
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 19 of 32
USING THE EXPRESSION BUILDER
The Expression Builder is a tool that can be used to help you build equations in queries, forms, and
reports. It lists all Access functions, operators, and fields.
Exercise:
To get the dollar amount on an item, we multiply: [Cost Per Unit] x [Quantity] x [Ship Rate]
There are two different ship rates based on weather they are domestic or foreign customers:
Domestic Customers (USA) pay 10%.
Foreign Customers pay 25%.
We will use the Expression Builder to create the following equation. Note that the expression builder
will also place the table names in the equation. This is acceptable.
1. From the Database window, click on the Tables tab.
2. Select the table: “Orders”.
3. From the menu, click on: INSERT - QUERY.
4. Select “Design View” and click on “Ok” .
5. Click on the “Show Tables” icon.
6. Add following tables: “Order Detail”, “General Candy Info”, and “Sweets Customers”
7. Close the Show Tables window.
8. Add the fields shown to the grid:
9. Click in the next available blank column in the field row (to the right of Cost Per Unit ).
Total: IIF([Country]="USA",[COST PER UNIT]*1.10,[COST PER UNIT]*1.25)*[Quantity]
Condition True section.
(Cost with 10%)
False section.
(Cost with 25%)
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 20 of 32
10. Click on the “Build” icon:
11. Double click the Functions folder.
12. Click Built-in-Functions.
13. Select the All category.
14. Select IIF in the right column.
15. Click on the “PASTE” button.
16. In the expression window, click on
the <<expr>> to select it.
17. Double click the Tables folder.
18. Click the table: “Sweets Customers”
19. Click the field: Country
20. Click on “PASTE”
21. Place the cursor after: [Country]
22. Click the = operator.
23. Select the <<expr>>
24. Type: “USA” (in quotes).
25. Select <<truepart>>.
26. Click in Tables folder: “General Candy Info”.
27. Select the field: Cost Per Unit
28. Click on “PASTE”
Construct your equations here.
Common operators.
Click add to the
equation.
Click a folder to
view its contents.
Double click folders
with + signs to
expand the folder.
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 21 of 32
29. Click after [Cost Per Unit].
30. Click on the multiplication operator: * 31. Select the <<Expr>> code.
32. Type: 1.10
33. Select <<falsepart>>
34. Select the General Candy Info folder again.
35. Select the field Cost Per Unit and click on “PASTE”
36. Place the cursor after [Cost Per Unit]
37. Click on the multiplication operator: * 38. Select the <<Expr>> code.
39. Type: 1.25
40. Click after the closing )
41. Click the multiplication operator: *
42. Click on the Tables folder.
43. Select the table: “Order Detail”
44. Select the field: Quantity
45. Click on “PASTE”
46. Click in front of the equation.
47. Type: Total: 46. Click on “Ok”
47. Press “ENTER” to leave the field.
48. Run the query .
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 22 of 32
BACKING UP A TABLE
Making a backup copy of a table is invaluable if you wish to protect your data from accidental
deletions. Note that this only backs up the table and not the entire database file. If you wish to back up
the entire file and all the objects it contains (tables, forms, reports, queries), see below “Backing up a
Database File”.
1. Click on the Tables tab in the main Database window.
2. Select but do not open the table: “General Candy Info”
3. From the menu, click on: EDIT - COPY
4. From the menu, click on: EDIT - PASTE
5. Type in a name for the new table.
6. Select the option: Structure and Data
7. Click on “Ok” .
A copy of General Candy Info is created within the
Database window.
BACKING UP A DATABASE FILE
This uses the Window’s Explorer to backup the entire database file including all tables, forms, reports,
and queries it contains.
1. From the Windows Task Bar, right-mouse click the word “START”.
2. From the pop-up list, select: Explorer
3. Browse to and select the file to be copied.
4. From the menu, click on: EDIT - COPY
5. From the menu, click on: EDIT - PASTE
A copy of the file is created. The copy will be named: Copy of Original File Name
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 23 of 32
ACTION QUEIRES
By default, queries are SELECT queries which means they select data but they do not alter it.
ACTION queries are another type of query which do alter data in some way.
It is strongly recommended that before performing an action query that you back up your table.
You can not undo an action query!
There are several types of action queries:
Update Query
Update queries can be used to make global changes to your records based upon a criteria. For example,
you would like to raise the credit limit of those people who live in the USA by 10%.
Delete Query
Delete queries can be used to delete records that meet a specific criteria. For example, you would like
to delete the records of all customers who have not make a purchase in the last five years.
Make Table Query
Make table queries are used to make a new table. The new table can include some or all fields from the
old table(s) and some or all of the records from the old table(s). For example, you wish to make a table
of only those people who have made purchases under $5.
Append Query
Append Queries take data from one table and add it to another. This is useful when you have two
people doing data entry into the same type of table and you now wish to merge the two together.
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 24 of 32
Update Query: Raise the Credit Limit of all U.S. Customers by $500
This query takes what already exists in a field and alters it mathematically. Note that to include the old
data in some form in the update, the old field name must be present in the calculation.
1. From the Database window, click on the Tables tab.
2. Select the table: “Sweets Customers”
3. From the menu, select: INSERT - QUERY.
4. Select “Design View”
5. Construct the query as shown.
6. Run the query to verify
that this will only affect people
living in the USA.
Note that if “USA” was not present, $500 would be added to the credit limit of all records.
7. Return to “Design View”
8. From the menu, click on: QUERY - UPDATE QUERY
9. Place the code in the Update To row in the Credit Limit column.
Note that the code is: [Credit Limit]+500. If you would have typed just: +500 or 500 without the
[Credit Limit] in front of it, Access would have replaced all U.S. customer’s credit limit with 500.
10. Run the query .
11. Click on “Yes” if your
message reads as follows.
Note that if you run this query
again, you will add another $500
to the credit limits.
Also note that Access does not
display the results of the action query. To see the results, change the query back to a select query and
run it again.
12. From the menu, click on: INSERT - QUERY
13. Select: Select Query
14. Run the query .
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 25 of 32
Update Query:
Replace the Manufacturer “General Foods” with “Bob’s Foods”
This update query takes data in a text field and replaces it with other data.
1. From the Database window, click on the Tables tab.
2. Select the table: “General Candy Info”
3. From the menu, select: INSERT - QUERY.
4. Select “Design View”
5. Construct the query as shown.
6. Run the query to verify that this will only affect General Foods manufactureres.
Note that if “General Foods” was not present, all manufacturers would change to “Bob’s Foods”.
7. Return to “Design View”
8. From the menu, click on: QUERY - UPDATE QUERY
9. Place the code in the Update To row in the Manufacturer column.
Note that it is not necessary to include the old field name in the
code. This is because we are completely replacing old data
rather than altering it as in the last example.
10. Run the Query .
11. Click on “Yes” if your message reads as follows.
Note that Access does not display
the results of the action query. To
see the results, change the query
back to a select query and run it
again. Be sure to remove the criteria
“General Foods” or the query will
return an empty record set. (General
Foods no longer exists.)
12. From the menu, click on: INSERT - QUERY
13. Select: Select Query
14. Run the query .
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 26 of 32
Make Table Query:
Create a Table of Only Those Customers Living in the UK
This query will copy only those customers who live in the UK into a new table called “UK Customers”.
Note that you can include any columns desired in the new table but because we will be using this table
in a later exercise, we will place all of the columns in the new table.
1. From the Database window, click on the Tables tab.
2. Select the table: “Sweets Customers”
3. From the menu, select: INSERT - QUERY.
4. Select “Design View”
5. Double click the table name “Sweets Customers” (All fields are selected.)
6. Click & drag any one of the selected fields to the first column of the data grid. (All fields are
placed on the grid.)
7. Move to the Country column and place the letters
UK in the criteria row.
8. Run the query to verify that only UK customers will be
placed into the new table.
9. Return to “Design View”
10. From the menu, click on: QUERY - MAKE TABLE QUERY
11. Fill out the pop-up box as shown.
12. Click on “Ok” .
13. Run the Query .
14. When the information box opens,
click on “Yes” to create the new
table.
To see the new table:
a. Close the query. (Save it if you wish.)
b. Click on the Tables tab in the main Database window.
c. Double click the new table (“UK Customers”) to open it.
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 27 of 32
Delete Query: Delete all UK Customers from Sweets Customers
From the “Sweets Customers” table, this query will delete the records of any customer living in the
UK.
1. In the Database window, click on the Tables tab.
2. Select the table: “Sweets Customers”
3. From the menu, select: INSERT - QUERY.
4. Select “Design View”
5. Set up the query grid as shown. Note
that placing Company Name on the
grid was not necessary.
6. Run the query to verify only UK
customers will be deleted.
7. Return to “Design View”
8. From the menu, click on: QUERY - DELETE QUERY
Note that a Delete row is added and the
“where” keyword is placed in the row for you.
9. Run the query .
10. Click “Yes” when you get the warning
about deleting rows.
Note that Access does not display the results of the action query. To see the results, change the query
back to a SELECT query and run it again. Be sure to remove the criteria “UK” or the query will
return an empty record set. (Records for UK customers no longer exist.)
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 28 of 32
Append Query: Copy Records from One Table to Another
This query will copy records from the table “UK Customers” to the table “Sweets Customers”.
Append Guidelines
The table being appended to should always be backed up prior to executing the append.
The query is based upon the table where data is being appended (copied) from.
If you get an error message when attempting to append, consider any complications caused by a
keyed table. For example, if you are appending into a table that has a primary key set but you are
not appending any data into keyed field, Access will not allow the append because keyed fields can
not contain blanks. Another problem sometimes arises when data is being appended into the keyed
field. If the data being added duplicates data that is already in the keyed field, then Access will not
allow the append.
When appending from one table to another, if the field names are an exact match, Access will place
data from one table into its corresponding field in the other table. When the field names do not
match, the user must tell Access the corresponding fields.
The field names do not have to be in the same order in both tables.
Not all fields (columns) need to be copied into the table.
Not all records need to be copied into the new table. You can append based upon a criteria.
Appending has no affect on the table being appending from but can dramatically affect the table
being appended to.
1. In the Database window, click on the Tables tab.
2. Select the table: “UK Customers” (Created above in the Make Table Query example.)
3. From the menu, select: INSERT - QUERY.
4. Select “Design View”
5. Double click the table name “UK Customers” (All fields are selected.)
6. Click & drag any one of the selected fields to the first column of the data grid.
(All fields are placed on the grid and will be appended to the Sweets Customers table.)
7. From the menu, click on: QUERY - APPEND QUERY
8. Select “Sweets Customers” as the table you are appending to.
9. Make sure Current Database is selected and click on “Ok” .
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 29 of 32
This is the field being appended from.
This is the table being appended from.
This is the field data is being
copied to. Note that if the field
names between the two tables were
not spelled the same, you would
need to select the field name
yourself from a drop down list.
10. Verify that the From and To field names match for each column.
11. Run the query .
12. Click “Yes” when you get the message about the append taking place.
13. Close the query and save if desired.
14. Open the “Sweets Customers” table.
There should be customers listed who do live in the UK.
CROSSTAB QUERIES A crosstab query takes data that is in a list and summarizes it into columns and rows. They are useful
for performing data analysis by category. A crosstab query is not an action query and has no effect on
the table it is based on. To perform a crosstab query, at least 3 different columns are needed. Crosstab
queries are simple to create using the “Crosstab Query Wizard”. Crosstab queries can also be created
manually but that procedure can be somewhat tedious.
In this example, we have a list of movies made between 1972 and 1994. The list contains the name of
the movie, its rating, release date, profit made, production company, and the type of movie it is. We
would like to know the total profit for each year broken down by movie rating.
1. In the main Database window, click on the Tables tab.
2. From the menu, click on: INSERT - QUERY
3. Select Crosstab Query Wizard and click on “Ok” .
4. When asked for the table to base the query on, select the table “MOVIES” and click “NEXT” .
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 30 of 32
5. Select Rating for the Row Headings and then click on “NEXT” .
6. Select Year for the Column Headings and then click on “NEXT” .
The Row Headings have been set to Rating and the Column Headings have been set to Years . The data that goes where these two intersect must now be selected.
7. Select Profit as the field to be calculated.
8. Select Sum as the function which is to be performed upon the profit field.
9. Make sure Yes, include row sums is checked.
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 31 of 32
10. Click on “NEXT” .
11. Type a name for the query.
12. Click on “FINISH” .
You data is now summarized into columns and rows as shown below. Where Rating and Year intersect is the total profit for that category of movie. For example, the total profit for all G rated
moves in 1972 was $109,890,890.00.
Total of Profit is a special summary column which contains the total profit for all movies
regardless of the year it was made, broken down by rating. For example, the total profit for all G rated
movies was $2,185,869,138.00.
USC - Marshall School of Business AIS – User Support
Access_2002-Adv_Queries.doc April 23, 2003 Page 32 of 32
AUTOMATICALLY DELETE DUPLICATE RECORDS
This procedure will delete all duplicate records from a table leaving only one record with the original
data. The process takes several steps and involves:
Creating a duplicate structure of the table that contains duplicates without any data in it.
Making primary keys of all the fields that contain duplicates
Running an append query from the original table to the new table.
Because keyed tables do not allow duplicate records, Access will not allow any duplicates to be copied
to the new table. In other words, you will get only on instance of the duplicate record.
Step A: Copy the Structure of the Table Containing Duplicates
1. Click on the Tables tab.
2. Right mouse click “Sweets Customers” and select: Copy
3. Click the “Paste” button.
4. Name the table: “Sweets Customers without Duplicates”
5. Select: Structure Only
6. Click “Ok” .
Step B: Assign Primary Keys to Duplicate Fields
1. Open “Sweets Customers without Duplicates” in “Design View” .
2. Assign a primary key to the Company Name field.
3. Save and close the table.
Step C: Append from the Original Table to the New Table
1. Single click the “Sweets Customers” table.
2. From the menu, click: INSERT - QUERY - DESIGN VIEW
3. In the field list box, double click the asterisk (*) to tell Access to include all columns.
4. From the menu, select: QUERY - APPEND QUERY
5. Select “Sweets Customers without
Duplicates” as the table to append to.
6. Click on “Ok” .
7. Run the query .
8. Click “Yes” to append all records.
9. Click “Yes” when access give you an error
message.
If you open “Sweets Customers without
Duplicates” , it now contains records but no duplicates.