Presentation is loading. Please wait.

Presentation is loading. Please wait.

Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations.

Similar presentations


Presentation on theme: "Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations."— Presentation transcript:

1 Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations

2 Querying : The process of asking the database for specific information
Thus far we have written queries that can show part of data tables by displaying only specific records and fields. These are known as Select Queries. Relational & Boolean operators in criteria Use of wildcards In addition to selecting specific information, queries can be used to manipulate data – sorting and aggregating information, and performing calculations.

3 Consider the following database :
Products: Suppliers: Primary Key Foreign Key

4 Sorting Tables in a Query
Use the Sorting line - Ascending or Descending to sort the resulting Query Table To sort multiple fields The computer will apply sort precedence for sorting fields listed on query from left to right. Sorting can be combined with any of the filtering techniques (criteria) we have already discussed.

5 List the product name, category, supplier id and units in stock for all products. The list should be ordered by units in stock - largest quantity first, and then by SupplierID. Products CategoryID ProductName Unitsinstock SupplierID ProductID Price Major Sort Minor Sort

6 The “major” sort is placed to the left of the “minor” sorts in the design view
Major sort Units in Stock Minor sort Supplier ID Major sort Supplier ID Minor sort Units in Stock Here First the supplier id field is sorted Here First the units in stock field is sorted

7 To Aggregate fields in a Query use the Group by feature
Sum Avg Min Max Count StDev Var First Last Expression Where Use the Group by feature to summarize a field – calculating its average, min, max, sum etc. Select the group(s) you wish to aggregate - Access will automatically group by the lowest detail level field. In the desired field(s) select the function (sum, average, etc) using the pull down menu

8 Write a query to summarize by category the number of different types of products and the total units in stock Products CategoryID ProductName Unitsinstock SupplierID ProductID

9 The resulting dynaset: Notice the new name given to the field’s
for Sum and Count Can we include Product name in this query? How could we get the average number of units in stock for products by category?

10 Query design grid's Total Row options:
Group By: Define the groups you want to perform the calculations for. Aggregate Functions: Sum, Avg, Min, Max etc. Where: Specify criteria for a field, so only those records that meet this criteria will be included in the aggregates. For example specify total units in stock by category for only supplier #1. Expression: Use if a calculated field is required in the query. Expressions will be calculated AFTER the fields are aggregated.

11 The resulting dynaset:
Write a query to summarize by category the number of units in stock for Supplierid 1 or 2. Products CategoryID ProductName Unitsinstock SupplierID ProductID The resulting dynaset:

12 Calculated Fields in Queries
The Expression Builder is a feature in MS Access that can be used with a Query to build numerical or boolean expressions: You may use any fields from existing tables or queries as part of the expression The builder constructs the expression based on which field, values and operators you choose Brackets enclose table and field names. Table/Query names are followed by an exclamation point. [table name]![field name]

13 The Expression Builder
Write your equation here Expr1:[Product]![Unitsinstock] * [Product]![UnitPrice] Select here from a list of tables, queries & other objects for use in expression Select here for Fields and operators

14 Write a query to calculate the value of your inventory by product
Write a query to calculate the value of your inventory by product. Include the product name, units in stock and value. Name your query Totval. Products CategoryID ProductName Unitsinstock SupplierID ProductID Price Calculated Field If a field is already included on this QBE Grid, the table/query name would not be required to identify the field.

15 If prices are increased by 10% on all items, estimate the new total value of your inventory by product Totval ProductName Unitsinstock Expr1 Here we are using the previously saved query instead of table Expr1: can be renamed to a new name. An expression can also use a value from a calculation in a previous query.

16 If you do not have data entered in a field, the field is said to contain a Null value. Calculations which include a Null value will result in a Null value. Null value + 5 = Null Value A field defined as a primary key can not have a Null value. The Nz function allows you to change null values Nz ([variant], valueifnull) NZ will default to 0 if valueifnull is omitted

17 A truck has just come in with 5 of every item you sell
A truck has just come in with 5 of every item you sell. Write a query to determine the new inventory of each item. Sort the list by number of units in stock, the most first. Products CategoryID ProductName Unitsinstock Price What is the value of this expression if units is stock is null?

18 Using Queries - Summary:
Select the fields to be shown - show box checked Select specific records by specifying criteria AND, OR, NOT operators used to specify multiple criteria in a single field Use Grid placement to indicate multiple criteria in multiple fields with same line for AND, using multiple criteria lines for OR Use wildcards * and ? to further specify text criteria to contain specific text string Sort resulting dynaset records for major/minor sorts – left to right grid placement determines sort order Use ∑ to toggle Total row to group and aggregate Use Expression Builder to easily enter formulas – using fields as operands.


Download ppt "Querying in Access Objectives: Learn how to use the Access Query Design Tool manipulate data in Access: Sorting data Aggregating Data Performing Calculations."

Similar presentations


Ads by Google