Professional Documents
Culture Documents
Contents
Creating Queries ................................................................................................................... 2 Single Table Query ............................................................................................................... 3 Multi-table Query................................................................................................................... 6 Adding Calculations ............................................................................................................ 11 Joining Text Fields .............................................................................................................. 12 Using the Expression Builder .............................................................................................. 16 Creating Queries using Other Queries ................................................................................ 19 Query using Totals and Group By ....................................................................................... 22
This tutorial assumes the Microsoft Access 2010 Tutorial has been completed to create the Basic Sales System Database itself. The 16 pages of this tutorial repeats the query part of the general tutorial
Page 1
Creating Queries
Now that the data has been added to the tables, it can be used to produce results related to a set of conditions given to it. This is known as queries. Queries can be from one table or from a number of tables. Queries Select Create Query Design
Steps 1. 2. 3. 4. 5. 6.
Select the required table(s) Select the fields required Include any conditions Select display requirements Run Query Save Query with a suitable name by closing it, or clicking the Save Button at the top of the Access screen
Important: When creating queries check that the results seem to be reasonable. When there is only a small dataset, look at the data and determine what the results should be before running the query and checking that the results obtained are the same.
Page 2
Select the CustomerFirstName, CustomerLastName and CustomerPhoneNumber by using the dropdown arrow in the Field row, by dragging the attribute from the Customer table shown, or by double clicking the attribute in the Customer table shown.
Page 3
Result
Changing the views: Datasheet shows the results SQL shows the query written in the SQL language (covered in DAT501) Design shows the query using the GUI view we started the query with.
Sorting Lets sort this query by the customers last name. Change the query to Design View mode and select Ascending for CustomerLastName
Page 4
Now the results are sorted by the customers last name Close the query and give it a suitable name when prompted (eg qCustomerPhoneList).
Page 5
Multi-table Query
Create a list of customer names and the dates they have purchased items. This is a query across 2 tables: Customer and Sales Click Create Query Design Add Customer and Sales Notice that this time, the relationship between the tables is also shown (a customer may have many sales, but a sale can only relate to one customer)
Select CustomerFirstName, CustomerLastName from the Customer table and the SaleDate from the Sale table. Sort by the sale date.
Result
Page 6
Using Criteria Both queries so far have returned all of the records from the tables selected. It is possible to select only some of the records by using the Criteria section. Adjust the query by showing only Bill Smiths sales by including Bill in Customer FirstName and Smith in CustomerLastName.
Result
Change the query to show only those sales which occurred in February 2012 This can be done by typing >= 1/2/12 And <= 29/2/12 in the SaleDate criteria. When the query is run, Access adds the # around each date.
Result
Page 7
Multi-table Query with 3 Tables Create a query which shows the Employee Name, Date, Quantity, and Inventory Name for sales for each Employee. Sort by Employee Last Name This query is across 3 tables: Employee, Inventory and Sale.
Page 8
Result
Save the query with a suitable name. Adjust the query to show only the sales which Gail Smith has made.
Result
Page 9
Multi-table Query with 4 Tables Create a query which shows the customer name, address, Inventory Name, Date, Quantity, Price, and Employee Name for each sale. Sort by customer last name.
Result
Save the query with a suitable name (qCustomerSale). Experiment with different conditions.
Page 10
Adding Calculations
The price shown in the Sale table represents the unit price for the items sold. It is possible to add the total price for the sale to the record by using a query. Values which can be calculated from the other data in the table are generally not included within the table due to the normalisation rules and to ensure an efficient database. Open qCustomerSale and rename it by going to File Save Object As (qCustomerSaleTotal).
The format to add a calculated attribute is Column Name: Table.Attribute * Table.Attribute In the first empty cell type TotalSalePrice: Sale.SaleQuantity*Sale.SaleUnitPrice
Once the query has been run, the calculation will show [ ] around the table and attribute names:
Page 11
Switch into Design View In the first empty cell type: CustomerName: CustomerFirstName & & CustomerLastName
Try combining city and postcode into 1 field. To really see the effect of this joining text fields, create a report which shows customer addresses using the separate CustomerFirstName, CustomerLastName attributes as well as the joined attribute CustomerName and see the differences.
Page 12
Activity Create the following queries. They will be used for the Report Tutorial Results are shown for each query but your results may be different depending on the data contained in your tables. qEmployeeFullName
Result:
Page 13
qCustomerDetails
Result:
Page 14
qCustomerSaleInvoice
Result:
Page 15
The active database is included and when this is expanded other elements from the database are shown:
Page 16
The Operators element gives access to the operators which can be used:
To use the expression builder Expand the current database and select the Sales table to gain access to the attributes available
Create the query by giving it a name and then selecting the expressions required
Click Ok
Page 17
Result
Page 18
Select qCustomerDetails, qCustomerSaleInvoice and qEmployeeFullName queries created above. Drag the CustomerID from qCustomersDetails to qCustomerSaleInvoice and the EmployeeID from qCustomerSaleInvoice to qEmployeeFullName to create the relationships below:
The creation of the relationships is an important step or else the query will match everything with everything (and I mean everything)!
Page 19
Depending on the data contained in the various tables, the following results may be produced Result:
It is this query which will be used to produce the invoice in the Reports tutorial.
Page 20
Parameter Queries
A parameter query is one where input of one or more field values is required to run the query. The request for data entry is placed in the criteria row. For example Create a query which returns the customer details only for the customer ID entered. Copy the query qCustomerDetail Select the query File Save Object As qCustomerDetailParameter Open the qCustomerDetailParameter in Design View Add the following as the Criteria for customerID: [Enter Customer ID]
Run the query The first thing which happens is a dialogue box asking for the customer ID to be entered Enter 1 and click OK Then the query is produced
A parameter query can be used as the basis for a report. This means when the report is opened, the user will be asked to input the required data and then the report will be run. It is possible to require more than 1 value to be entered just add more parameter values to the criteria line. Sometimes a dialogue box requesting the entry of a Parameter Value occurs when it is not wanted! This can happen if a textbox is used in a report or form instead of a label. A label allows text to be shown on the report or form but a textbox is used to get the data from the named database attribute. If Access cannot find the named attribute, it will ask for it from the user. These types of errors are generally related to design errors.
Page 21
qTotalSaleForCustomer is a query which will calculate the total sales a customer has Create the query by using the qCompleteSaleDetail query and the CustomerID, CustomerName and TotalSalePrice attributes. Run the query:
The following result is required if the total sales for each customer from the above are calculated:
Page 22
Adjust your query to show the Total row by selecting the Totals button on the Design tab. This adds a new line to bottom part of the query for Total and Group By for each field selected.
Change the Group By option for SaleTotal to Sum as shown below by clicking the down arrow.
It is possible to add additional attributes or amounts to the sum calculated above. For example adding $10 to the total of a customers sales (rather than adding $10 to each sale) Copy and rename the above query to qTotalSaleForCustomerPlus10 Use the qTotalSaleForCustomer query Add a new attribute called Plus10 as shown below
Result:
Page 23