Download presentation
Presentation is loading. Please wait.
Published byAlexandrina York Modified over 9 years ago
1
CS&E 1111 AcQueries Querying in Access Sorting data Aggregating Data Performing Calculations Objectives: Learn how to use the Access Query Design Tool manipulate data in Access:
2
CS&E 1111 AcQueries Querying : The process of asking the database for specific information l 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. l Relational & Boolean operators in criteria l Use of wildcards l In addition to selecting specific information, queries can be used to manipulate data – sorting and aggregating information, and performing calculations.
3
CS&E 1111 AcQueries Foreign Key Primary Key Products: Suppliers: Consider the following database :
4
CS&E 1111 AcQueries Sorting Tables in a Query Use the Sorting line - Ascending or Descending to sort the resulting Query Table l To sort multiple fields – The computer will apply sort precedence for sorting fields listed on query from left to right. l Sorting can be combined with any of the filtering techniques (criteria) we have already discussed.
5
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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 units in stock field is sorted Here First the supplier id field is sorted
7
CS&E 1111 AcQueries To Aggregate fields in a Query use the Group by feature Use the Group by feature to summarize a field – calculating its average, min, max, sum etc. l Select the group(s) you wish to aggregate - Access will automatically group by the lowest detail level field. l In the desired field(s) select the function (sum, average, etc) using the pull down menu Group by SumAvgMinMaxCountStDevVarFirstLastExpressionWhere
8
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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: l You may use any fields from existing tables or queries as part of the expression l The builder constructs the expression based on which field, values and operators you choose l Brackets enclose table and field names. Table/Query names are followed by an exclamation point. [table name]![field name ]
13
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries 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
CS&E 1111 AcQueries Using Queries - Summary: l Select the fields to be shown - show box checked l Select specific records by specifying criteria l AND, OR, NOT operators used to specify multiple criteria in a single field l Use Grid placement to indicate multiple criteria in multiple fields with same line for AND, using multiple criteria lines for OR l Use wildcards * and ? to further specify text criteria to contain specific text string l Sort resulting dynaset records for major/minor sorts – left to right grid placement determines sort order l Use ∑ to toggle Total row to group and aggregate l Use Expression Builder to easily enter formulas – using fields as operands.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.