1
Computer Science & Engineering 2111
Computer Science & Engineering 2111
Lecture 12Sorting, Summarizing and Calculating in Queries
Sorting Results 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 a query from left to right.
Sorting can be combined with any of the filtering techniques (criteria) we have already discussed.
2
3
Write a query to list the City and State of all clients The list should be sorted in ascending order by State and then by City.
Field City State City
Table Client Client Client
Sort Ascending Ascending
Show x x
Criteria
OR
OR
Query Name: NameSort Tables Required: ClientForeign Keys: None Join Type: None
Major Sort
Minor Sort
The data table
The resulting dynaset
4
5
You have decided to give all clients a 10% break on each charge they have made. Write a query to list the ClientID, each charge amount before the discount, the discount, and the charge amount after the discount .
Field ClientID Amount Discount: [Amount] * 0.10 New Charge: [Amount] – [Discount]
Table Client Charges
Sort
Show x x x x
Criteria
OR
OR
Query Name: DiscountCharges Tables Required: Client/ChargesForeign Keys: ClientID Join Type: Inner
What if we want to create a field in a query that is the result of a calculation?
The resulting dynasetNotice new field names
6
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.
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
Group by
Sum
Avg
Min
Max
Count
StDev
Var
First
Last
Expression
Where 7
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.
Expression: Use if a calculated field is required in the query. Expressions will be calculated AFTER the fields are aggregated.
8
9
Write a query that lists how many payments were made by each Client and their total payments. List the First Name, Last Name, the number of payments made, and the total dollar amount of these payments.
Query Name: PaymentSummary Tables Required: Client/PaymentsForeign Keys: ClientID Join Type: Inner
Field First Name Last Name ClientID Amount
Table Client Client Payments Payments
Total Group by Group by Count Sum
SortShow x x x x
CriteriaOROR
Notice new field names
10
The resulting dynaset
The data tables(partial view)
11
Write a query to summarize by Client, all of the charges made after 5/1/2008. Add a surcharge of 20% to these charges. List the First Name, Last Name, Original Charge and New Charge.
Query Name: ChargesSummary Tables Required: Client/ChargesForeign Keys: ClientID Join Type: InnerField First Name Last Name Amount ChargeDate New Charge: [SumOfAmount] * 1.20Table Client Client Charges Charges
Total Group by Group by Sum Where Expression
Sort
Show x x x x
Criteria > #5/1/2008#
OR
OR
Notice new field names
12
The resulting dynaset
The data tables
Top Related